EMAN2
spiderio.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 "spiderio.h"
33#include "geometry.h"
34#include "portable_fileio.h"
35#include "emassert.h"
36#include "util.h"
37#include "transform.h"
38#include <iostream>
39#include <ctime>
40#include <algorithm>
41
42using namespace EMAN;
43
44SpiderIO::SpiderIO(const string & fname, IOMode rw)
45: ImageIO(fname, rw),
46 first_h(0), cur_h(0),
47 is_big_endian(ByteOrder::is_host_big_endian())
48{
50}
51
53{
54 if (file) {
55 fclose(file);
56 file = 0;
57 }
58
59 if (first_h) {
60 free(first_h);
61 first_h = 0;
62 }
63
64 if (cur_h) {
65 free(cur_h);
66 cur_h = 0;
67 }
68}
69
70void SpiderIO::init()
71{
72 if (initialized) {
73 return;
74 }
77 initialized = true;
78
79 if (!is_new_file) {
80 first_h = static_cast < SpiderHeader * >(calloc(1, sizeof(SpiderHeader)));
81
82 if (fread(first_h, sizeof(SpiderHeader), 1, file) != 1) {
83 throw ImageReadException(filename, "SPIDER header");
84 }
85
87 throw ImageReadException(filename, "invalid SPIDER");
88 }
89
90 float nslice = first_h->nslice;
91
94
96 fclose(file);
97 file = 0;
98
99 file = fopen(filename.c_str(), "wb");
100 }
101 }
102
103 EXITFUNC;
104}
105
106bool SpiderIO::is_valid_spider(const void *first_block)
107{
108 return SpiderIO::is_valid(first_block);
109}
110
111bool SpiderIO::is_valid(const void *first_block)
112{
113 ENTERFUNC;
114 bool result = false;
115
116 if (first_block) {
117 const float *data = static_cast < const float *>(first_block);
118 float nslice = data[0];
119 float nrow = data[1];
120 float iform = data[4];
121 float nsam = data[11];
122 float labrec = data[12]; //NO. of records in file header
123 float labbyt = data[21]; //total NO. of bytes in header
124 float lenbyt = data[22]; //record length in bytes
125 float istack = data[23];
126
127 bool big_endian = ByteOrder::is_float_big_endian(nslice);
128 if (big_endian != ByteOrder::is_host_big_endian()) {
129 ByteOrder::swap_bytes(&nslice);
131 ByteOrder::swap_bytes(&iform);
133 ByteOrder::swap_bytes(&labrec);
134 ByteOrder::swap_bytes(&labbyt);
135 ByteOrder::swap_bytes(&lenbyt);
136 ByteOrder::swap_bytes(&istack);
137 }
138
139 if( int(nslice) != nslice || int(nrow) != nrow
140 || int(iform) != iform || int(nsam) != nsam
141 || int(labrec) != labrec || int(labbyt) != labbyt
142 || int(lenbyt) != lenbyt ) {
143 result = false;
144 }
145 else {
146 //now we expect this header to be an overall header for SPIDER
147 if( int(istack) > 0 ) {
148 result = true; //istack>0 for overall header, istack<0 for indexed stack of image
149 }
150 }
151
152 int ilabrec = static_cast<int>(labrec);
153 int ilabbyt = static_cast<int>(labbyt);
154 int ilenbyt = static_cast<int>(lenbyt);
155 if( ilabbyt != ilabrec * ilenbyt ) {
156 result = false;
157 }
158 }
159
160 EXITFUNC;
161 return result;
162}
163
166int SpiderIO::read_header(Dict & dict, int image_index, const Region * area, bool)
167{
168 ENTERFUNC;
169
170 bool is_read_overall_header = false;
171 if (image_index == -1) {
172 is_read_overall_header = true;
173 image_index = 0;
174 }
175
176 check_read_access(image_index);
177
178 if (!first_h) {
179 throw ImageReadException(filename, "empty spider header");
180 }
181
183
184 float size = first_h->nsam * first_h->nrow * first_h->nslice;
185 int overall_headlen = 0;
186
187 if (!is_read_overall_header) {
188 if (first_h->istack > 0) { //stack image
189 overall_headlen = (int) first_h->headlen;
190 }
191 else if(first_h->istack == SINGLE_IMAGE_HEADER) { //single 2D/3D image
192 if(image_index != 0) {
193 char desc[1024];
194 sprintf(desc, "For a single image, index must be 0. Your image index = %d.", image_index);
195 throw ImageReadException(filename, desc);
196 }
197 }
198 else { //complex spider image not supported
199 throw ImageFormatException("complex spider image not supported.");
200 }
201 }
202
203 size_t single_image_size = (size_t) (first_h->headlen + size * sizeof(float));
204 size_t offset = overall_headlen + single_image_size * image_index;
205
206 SpiderHeader *cur_image_hed;
207 if (offset == 0) {
208 cur_image_hed = first_h;
209 }
210 else {
211 cur_image_hed = static_cast < SpiderHeader * >(calloc(1, sizeof(SpiderHeader)));
212 portable_fseek(file, offset, SEEK_SET);
213
214 if (fread(cur_image_hed, sizeof(SpiderHeader), 1, file) != 1) {
215 char desc[1024];
216 sprintf(desc, "read spider header with image_index = %d failed", image_index);
217 throw ImageReadException(filename, desc);
218 }
219
220 become_host_endian((float *) cur_image_hed, NUM_FLOATS_IN_HEADER);
221
222 if (cur_image_hed->nsam != first_h->nsam || cur_image_hed->nrow != first_h->nrow
223 || cur_image_hed->nslice != first_h->nslice) {
224 char desc[1024];
225 sprintf(desc, "%dth image size %dx%dx%d != overall size %dx%dx%d",
226 image_index, (int)cur_image_hed->nsam, (int)cur_image_hed->nrow,
227 (int)cur_image_hed->nslice,
228 (int)first_h->nsam, (int)first_h->nrow, (int)first_h->nslice);
229 throw ImageReadException(filename, desc);
230 }
231 }
232
233
234 int xlen = 0, ylen = 0, zlen = 0;
235 EMUtil::get_region_dims(area, (int) cur_image_hed->nsam, &xlen, (int) cur_image_hed->nrow,
236 &ylen, (int) cur_image_hed->nslice, &zlen);
237
238 dict["nx"] = xlen;
239 dict["ny"] = ylen;
240 dict["nz"] = zlen;
241
242 dict["datatype"] = EMUtil::EM_FLOAT;
243
244 if(cur_image_hed->mmvalid == 1) {
245 dict["minimum"] = cur_image_hed->min;
246 dict["maximum"] = cur_image_hed->max;
247 dict["mean"] = cur_image_hed->mean;
248 dict["sigma"] = cur_image_hed->sigma;
249 }
250
251 dict["SPIDER.nslice"] = (int) cur_image_hed->nslice;
252 dict["SPIDER.type"] = (int) cur_image_hed->type;
253
254 dict["SPIDER.irec"] = cur_image_hed->irec;
255
256 dict["SPIDER.angvalid"] = (int)cur_image_hed->angvalid;
257 if((int)dict["SPIDER.angvalid"] != 0) {
258 dict["SPIDER.phi"] = cur_image_hed->phi;
259 dict["SPIDER.theta"] = cur_image_hed->theta;
260 dict["SPIDER.gamma"] = cur_image_hed->gamma;
261 }
262
263 dict["SPIDER.headrec"] = (int) cur_image_hed->headrec;
264 dict["SPIDER.headlen"] = (int) cur_image_hed->headlen;
265 dict["SPIDER.reclen"] = (int) cur_image_hed->reclen;
266
267 dict["SPIDER.dx"] = cur_image_hed->dx;
268 dict["SPIDER.dy"] = cur_image_hed->dy;
269 dict["SPIDER.dz"] = cur_image_hed->dz;
270
271 dict["SPIDER.istack"] = (int) cur_image_hed->istack;
272 if((int)dict["SPIDER.istack"] > 0) { //maxim only for overall header
273 dict["SPIDER.maxim"] = (int)cur_image_hed->maxim;
274 }
275 dict["SPIDER.imgnum"] = (int)cur_image_hed->imgnum;
276
277 dict["SPIDER.Kangle"] = (int)cur_image_hed->Kangle;
278 if((int)dict["SPIDER.Kangle"] == 1) {
279 dict["SPIDER.phi1"] = cur_image_hed->phi1;
280 dict["SPIDER.theta1"] = cur_image_hed->theta1;
281 dict["SPIDER.psi1"] = cur_image_hed->psi1;
282 }
283 else if((int)dict["SPIDER.Kangle"] == 2) {
284 dict["SPIDER.phi1"] = cur_image_hed->phi1;
285 dict["SPIDER.theta1"] = cur_image_hed->theta1;
286 dict["SPIDER.psi1"] = cur_image_hed->psi1;
287 dict["SPIDER.phi2"] = cur_image_hed->phi2;
288 dict["SPIDER.theta2"] = cur_image_hed->theta2;
289 dict["SPIDER.psi2"] = cur_image_hed->psi2;
290 }
291
292 dict["SPIDER.date"] = string(cur_image_hed->date).substr(0, 11);
293 dict["SPIDER.time"] = string(cur_image_hed->time).substr(0, 8);
294
295 dict["SPIDER.title"] = string(cur_image_hed->title);
296
297 if(cur_image_hed->scale>0) {
298 dict["SPIDER.scale"] = cur_image_hed->scale;
299 Dict dic;
300 dic.put("type", "spider");
301 dic.put("phi", cur_image_hed->phi);
302 dic.put("theta", cur_image_hed->theta);
303 dic.put("psi", cur_image_hed->gamma);
304 dic.put("tx", cur_image_hed->dx);
305 dic.put("ty", cur_image_hed->dy);
306 dic.put("tz", cur_image_hed->dz);
307 dic.put("scale", cur_image_hed->scale);
308 Transform * trans = new Transform(dic);
309 if(zlen<=1) {
310 dict["xform.projection"] = trans;
311 }
312 else {
313 dict["xform.align3d"] = trans;
314 }
315 if(trans) {delete trans; trans=0;}
316 }
317
318
329 if (offset != 0) {
330 if( cur_image_hed )
331 {
332 free(cur_image_hed);
333 cur_image_hed = 0;
334 }
335 }
336 EXITFUNC;
337 return 0;
338}
339
340
341int SpiderIO::write_header(const Dict & dict, int image_index, const Region* area,
342 EMUtil::EMDataType, bool use_host_endian)
343{
344 ENTERFUNC;
345
346 if (image_index<0) {
347 image_index = get_nimg();
348 }
349
350 if(is_new_file) { //for a new file write overall header first
351 write_single_header(dict, area, image_index, 0, first_h, OVERALL_STACK_HEADER, 1, 0, use_host_endian);
352 }
353 else {
355 }
356
357 if(!initialized) {
358 init();
359 }
360
361 if(!((int)dict["nx"]==first_h->nsam && (int)dict["ny"]==first_h->nrow &&
362 (int)dict["nz"]==first_h->nslice)) {
363 char desc[1024];
364 sprintf(desc, "%dth image size %dx%dx%d != overall size %dx%dx%d",
365 image_index, (int)dict["nx"], (int)dict["ny"], (int)dict["nz"],
366 (int)first_h->nsam, (int)first_h->nrow, (int)first_h->nslice);
367 throw ImageReadException(filename, desc);
368 }
369
370 if (!cur_h) {
371 cur_h = (SpiderHeader *) calloc(1, static_cast<size_t>(first_h->headlen));
372 }
373
374 int MAXIM;
375 float size = first_h->nsam * first_h->nrow * first_h->nslice;
376 size_t single_image_size = (int) (first_h->headlen + size * sizeof(float));
377 size_t offset;
378 if(image_index == -1) { //append image
379 offset = (int) first_h->headlen + single_image_size * (int)first_h->maxim;
380 MAXIM = (int)first_h->maxim + 1;
381 }
382 else {
383 offset = (int) first_h->headlen + single_image_size * image_index;
384 MAXIM = image_index>=(int)first_h->maxim ? image_index+1 : (int)first_h->maxim;
385 }
386
387 //update overall header
388 if(MAXIM > (int)first_h->maxim) {
389 portable_fseek(file, 0, SEEK_SET);
390 write_single_header(dict, area, image_index, 0, first_h, OVERALL_STACK_HEADER, MAXIM, 0, use_host_endian);
391 }
392
393 portable_fseek(file, offset, SEEK_SET);
394 write_single_header(dict, area, image_index, offset, cur_h, SINGLE_IMAGE_HEADER, 0, image_index+1, use_host_endian);
395
396 EXITFUNC;
397 return 0;
398}
399
400int SpiderIO::write_single_header(const Dict & dict, const Region *area, int image_index, size_t offset,
401 SpiderHeader *& hp, int ISTACK, int MAXIM, int IMGNUM, bool use_host_endian)
402{
403 ENTERFUNC;
404
405 check_write_access(rw_mode, image_index);
406
407 if (area) {
408 check_region(area, FloatSize(hp->nsam, hp->nrow, hp->nslice), is_new_file);
409 EXITFUNC;
410 return 0;
411 }
412
413 int nx = dict["nx"];
414 int ny = dict["ny"];
415 int nz = dict["nz"];
416
417 size_t header_size = sizeof(SpiderHeader);
418 size_t record_size = nx * sizeof(float);
419 size_t num_records = (header_size % record_size) == 0 ? header_size / record_size : header_size / record_size + 1;
420 size_t header_length = num_records * record_size;
421
422 if (!hp) {
423 hp = static_cast < SpiderHeader * >(calloc(1, header_size));
424 }
425
426 hp->angvalid = 0;
427 hp->scale = 1.0;
428 hp->istack = (float)ISTACK;
429 hp->nslice = (float)nz;
430 hp->nsam = (float)nx;
431 hp->nrow = (float)ny;
432
433 hp->max = dict["maximum"];
434 hp->min = dict["minimum"];
435 hp->mean = dict["mean"];
436 hp->sigma = dict["sigma"];
437 hp->mmvalid = 1;
438
439 if(nz<=1 && dict.has_key("xform.projection")) {
440 hp->angvalid = 1;
441 Transform * t = dict["xform.projection"];
442 Dict d = t->get_params("spider");
443 hp->phi = d["phi"];
444 hp->theta = d["theta"];
445 hp->gamma = d["psi"];
446 hp->dx = d["tx"];
447 hp->dy = d["ty"];
448 hp->dz = d["tz"];
449 hp->scale = d["scale"];
450 if(t) {delete t; t=0;}
451 }
452 else if(nz>1 && dict.has_key("xform.align3d")) {
453 hp->angvalid = 1;
454 Transform * t = dict["xform.align3d"];
455 Dict d = t->get_params("spider");
456 hp->phi = d["phi"];
457 hp->theta = d["theta"];
458 hp->gamma = d["psi"];
459 hp->dx = d["tx"];
460 hp->dy = d["ty"];
461 hp->dz = d["tz"];
462 hp->scale = d["scale"];
463 if(t) {delete t; t=0;}
464 }
465
466 if(nz == 1) {
467 hp->type = IMAGE_2D;
468 }
469 else {
470 hp->type = IMAGE_3D;
471 }
472
473// complex image file not supported in EMAN2
474
475 hp->reclen = (float)record_size;
476 hp->headrec = (float)num_records;
477 hp->headlen = (float)header_length;
478
479 if(ISTACK == OVERALL_STACK_HEADER) {
480 hp->maxim = (float)MAXIM;
481 }
482 hp->irec = (float)(num_records + ny*nz);
483
484 hp->imgnum = (float)IMGNUM;
485
486 time_t tod;
487 time(&tod);
488 struct tm * ttt = localtime(&tod);
489 char ctime[9];
490 char cdate[12];
491 strftime(ctime, 9, "%H:%M:%S", ttt);
492 std::copy(&ctime[0], &ctime[8], hp->time);
493 strftime(cdate, 12, "%d-%b-%Y", ttt);
494 std::copy(&cdate[0], &cdate[11], hp->date);
495
496 if(dict.has_key("SPIDER.title")) {
497 string title = static_cast<string>(dict["SPIDER.title"]);
498 std::copy(&(title[0]), &(title[title.length()]), hp->title);
499 }
500
501 portable_fseek(file, offset, SEEK_SET);
502
503 if(use_host_endian) {
504 if (fwrite(hp, header_size, 1, file) != 1) {
505 throw ImageWriteException(filename, "write spider header failed");
506 }
507 }
508 else { //swap byte order
509 SpiderHeader * hp2 = new SpiderHeader(*hp);
511 if (fwrite(hp2, header_size, 1, file) != 1) {
512 throw ImageWriteException(filename, "write spider header failed");
513 }
514 if(hp2) {delete hp2; hp2=0;}
515 }
516
517 size_t pad_size = header_length - header_size;
518 char *pad = static_cast < char *>(calloc(pad_size, 1));
519 fwrite(pad, pad_size, 1, file);
520 if( pad )
521 {
522 free(pad);
523 pad = 0;
524 }
525
526 EXITFUNC;
527 return 0;
528}
529
530
531int SpiderIO::read_data(float *data, int image_index, const Region * area, bool)
532{
533 ENTERFUNC;
534
535 check_read_access(image_index, data);
536
537 check_region(area, FloatSize((int) first_h->nsam, (int) first_h->nrow,
538 (int) first_h->nslice), is_new_file,false);
539
540 int overall_headlen = 0;
541 if (first_h->istack >0) { //for over all header length
542 overall_headlen = (int) first_h->headlen;
543 }
544 else {
545 if(image_index != 0) {
546 char desc[256];
547 sprintf(desc, "For single image, index must be 0. Your image index = %d.", image_index);
548 throw ImageReadException(filename, desc);
549 }
550 }
551
552 size_t size = static_cast < size_t > (first_h->nsam * first_h->nrow * first_h->nslice);
553 size_t single_image_size = static_cast < size_t > (first_h->headlen + size * sizeof(float));
554 off_t offset = overall_headlen + single_image_size * image_index;
555 portable_fseek(file, offset, SEEK_SET);
556
557 portable_fseek(file, (int) first_h->headlen, SEEK_CUR);
558
559#if 1
560 EMUtil::process_region_io(data, file, READ_ONLY, 0, sizeof(float),
561 (int) first_h->nsam, (int) first_h->nrow,
562 (int) first_h->nslice, area);
563#endif
564#if 0
565 unsigned int nz = static_cast < unsigned int >(first_h->nslice);
566 int sec_size = static_cast < int >(first_h->nsam * first_h->nrow * sizeof(float));
567
568 if (fread(data, sec_size, nz, file) != nz) {
569 LOGERR("Incomplete SPIDER data read");
570 return 1;
571 }
572#endif
573
574 int xlen = 0, ylen = 0, zlen = 0;
575 EMUtil::get_region_dims(area, (int) first_h->nsam, &xlen, (int) first_h->nrow, &ylen,
576 (int) first_h->nslice, &zlen);
577
578 int data_size = xlen * ylen * zlen;
579 become_host_endian(data, data_size);
580 EXITFUNC;
581 return 0;
582}
583
584int SpiderIO::write_data(float *data, int image_index, const Region* area,
585 EMUtil::EMDataType, bool use_host_endian)
586{
587 ENTERFUNC;
588
589 if(!cur_h) {
590 throw ImageWriteException(filename, "Please write header before write data");
591 }
592
594 throw ImageWriteException(filename, "Cannot mix single spider and stack spider");
595 }
596
597 size_t offset;
598 float size = first_h->nsam * first_h->nrow * first_h->nslice;
599 size_t single_image_size = (int) (first_h->headlen + size * sizeof(float));
600 offset = (int) first_h->headlen + single_image_size * image_index + (int) first_h->headlen;
601
602 swap_data(data, (size_t)size);
603
604 write_single_data(data, area, cur_h, offset, image_index, (int)first_h->maxim+1, use_host_endian);
605
606 EXITFUNC;
607 return 0;
608}
609
610
611void SpiderIO::flush()
612{
613 fflush(file);
614}
615
616int SpiderIO::write_single_data(float *data, const Region * area, SpiderHeader *& hp,
617 size_t offset, int img_index, int max_nimg, bool use_host_endian)
618{
619 ENTERFUNC;
620
621 check_write_access(rw_mode, img_index, max_nimg, data);
622
623 if (area) {
624 check_region(area, FloatSize(hp->nsam, hp->nrow, hp->nslice), is_new_file);
625 }
626
627 if (!hp) {
628 throw ImageWriteException(filename, "NULL image header");
629 }
630
631 portable_fseek(file, offset, SEEK_SET);
632
633 int size = (int)(hp->nsam * hp->nrow * hp->nslice);
634 if(!use_host_endian) {
635 ByteOrder::swap_bytes(data, size);
636 }
637
638 // remove the stuff in #if 0 ... #endif if the following works.
639 EMUtil::process_region_io(data, file, WRITE_ONLY,0, sizeof(float),
640 (int) hp->nsam, (int) hp->nrow,
641 (int) hp->nslice, area);
642
643 EXITFUNC;
644 return 0;
645}
646
647void SpiderIO::swap_data(float *data, size_t size)
648{
649 if (data && need_swap()) {
650 ByteOrder::swap_bytes(data, size);
651 }
652}
653
655{
656 if (header && need_swap()) {
658 }
659}
660
662{
663 int type = static_cast<int>(first_h->type);
664 if (type == IMAGE_2D_FFT_ODD
665 || type == IMAGE_2D_FFT_EVEN
666 || type == IMAGE_3D_FFT_ODD
667 || type == IMAGE_3D_FFT_EVEN
668 ) return true;
669 return false;
670}
671
673{
674 init();
675 return is_big_endian;
676}
677
679{
680 init();
681 if (!first_h) {
683 return 0;
684 }
685 else if (first_h->istack > 0) { //image stack
686 return static_cast < int >(first_h->maxim);
687 }
688 else if (first_h->istack == SINGLE_IMAGE_HEADER) { //single 2D/3D image
689 return 1;
690 }
691 else { //complex image
692 throw ImageFormatException("complex spider image not supported.");
693 }
694}
695
697{
699 return true;
700 }
701 return false;
702}
ByteOrder defines functions to work on big/little endian byte orders.
Definition: byteorder.h:59
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_float_big_endian(float small_number)
given a small floating number, return whether the number is in big endian or not.
Definition: byteorder.cpp:59
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
void put(const string &key, EMObject val)
Put the value/key pair into the dictionary probably better to just use operator[].
Definition: emobject.h:545
bool has_key(const string &key) const
Ask the Dictionary if it as a particular key.
Definition: emobject.h:511
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
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.
FILE * file
Definition: imageio.h:354
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.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
int write_single_data(float *data, const Region *area, SpiderHeader *&hp, size_t offset, int img_index, int max_nimg, bool use_host_endian=true)
write a single image data
Definition: spiderio.cpp:616
int get_nimg()
get the number of images in this stacked SPIDER image
Definition: spiderio.cpp:678
SpiderHeader * cur_h
Definition: spiderio.h:243
bool is_new_file
Definition: spiderio.h:246
virtual bool is_valid_spider(const void *first_block)
check the data block to see if it represents valid stacked SPIDER image file header
Definition: spiderio.cpp:106
void swap_header(SpiderHeader *header)
Definition: spiderio.cpp:654
void swap_data(float *data, size_t nitems)
Definition: spiderio.cpp:647
@ OVERALL_STACK_HEADER
Definition: spiderio.h:194
@ SINGLE_IMAGE_HEADER
Definition: spiderio.h:193
@ NUM_FLOATS_IN_HEADER
Definition: spiderio.h:195
bool need_swap() const
Definition: spiderio.cpp:696
static bool is_valid(const void *first_block)
Definition: spiderio.cpp:111
int write_single_header(const Dict &dict, const Region *area, int image_index, size_t offset, SpiderHeader *&hp, int ISTACK, int MAXIM=1, int IMGNUM=1, bool use_host_endian=true)
write a SPIDER header to spider_file
Definition: spiderio.cpp:400
bool is_big_endian
Definition: spiderio.h:245
SpiderHeader * first_h
Definition: spiderio.h:242
A Transform object is a somewhat specialized object designed specifically for EMAN2/Sparx storage of ...
Definition: transform.h:75
Dict get_params(const string &euler_type) const
Get the parameters of the entire transform, using a specific euler convention.
Definition: transform.cpp:479
static bool is_file_exist(const string &filename)
check whether a file exists or not
Definition: util.cpp:253
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
Definition: emassert.h:42
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
#define ImageReadException(filename, desc)
Definition: exception.h:204
#define ImageFormatException(desc)
Definition: exception.h:147
#define ImageWriteException(imagename, desc)
Definition: exception.h:223
#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)
float imgnum
imgnum is only used in a stacked image header.
Definition: spiderio.h:152
float mmvalid
max/min flag.
Definition: spiderio.h:110
float maxim
maxim is only used in the overall header for a stacked image file.
Definition: spiderio.h:148
float type
file type: 1 : 2D image; 3 : 3D volume; -11 : 2D Fourier, mixed radix odd.
Definition: spiderio.h:103
float istack
istack = 0 for simple 2D or 3D (non-stack) files.
Definition: spiderio.h:141