EMAN2
sspiderio.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 "sspiderio.h"
33#include "geometry.h"
34#include "portable_fileio.h"
35
36#include <iostream>
37
38using namespace EMAN;
39
40SingleSpiderIO::SingleSpiderIO(const string & fname, IOMode rw)
41: SpiderIO(fname, rw)
42{
43}
44
45
47{
48}
49
50
51int SingleSpiderIO::write_header(const Dict & dict, int , const Region* area,
52 EMUtil::EMDataType, bool use_host_endian)
53{
54 size_t offset = 0;
55 int image_index = 0;
56// if(!use_host_endian) {
57// ByteOrder::swap_bytes((float*)first_h, NUM_FLOATS_IN_HEADER);
58// }
59 return write_single_header(dict, area, image_index, offset, first_h, SINGLE_IMAGE_HEADER, 1, 1, use_host_endian);
60}
61
62
63int SingleSpiderIO::write_data(float *data, int , const Region* area,
64 EMUtil::EMDataType, bool use_host_endian)
65{
66 size_t offset = (int) first_h->headlen;
67 return write_single_data(data, area, first_h, offset, 0, 1, use_host_endian);
68}
69
70
71bool SingleSpiderIO::is_valid(const void *first_block)
72{
74 bool result = false;
75
76 if (first_block) {
77 const float *data = static_cast < const float *>(first_block);
78 float nslice = data[0];
79 float nrow = data[1];
80 float iform = data[4];
81 float nsam = data[11];
82 float labrec = data[12]; //NO. of records in file header
83 float labbyt = data[21]; //total NO. of bytes in header
84 float lenbyt = data[22]; //record length in bytes
85 float istack = data[23];
86
87 bool big_endian = ByteOrder::is_float_big_endian(nslice);
88 if (big_endian != ByteOrder::is_host_big_endian()) {
89 ByteOrder::swap_bytes(&nslice);
93 ByteOrder::swap_bytes(&labrec);
94 ByteOrder::swap_bytes(&labbyt);
95 ByteOrder::swap_bytes(&lenbyt);
96 ByteOrder::swap_bytes(&istack);
97 }
98
99 if( int(nslice) != nslice || int(nrow) != nrow
100 || int(iform) != iform || int(nsam) != nsam
101 || int(labrec) != labrec || int(labbyt) != labbyt
102 || int(lenbyt) != lenbyt ) {
103 result = false;
104 }
105 else {
106 int itype = static_cast < int >(iform);
107 if( int(istack) != SINGLE_IMAGE_HEADER ) {
108 result = false; //istack>0 for overall header, istack<0 for indexed stack of image
109 }
110 else if( itype == IMAGE_2D_FFT_ODD || itype == IMAGE_2D_FFT_EVEN
111 || itype == IMAGE_3D_FFT_ODD || itype == IMAGE_3D_FFT_EVEN ) {
112 result = false; //Complex SPIDER image not supported in EMAN2
113 }
114 else if (itype == IMAGE_2D || itype == IMAGE_3D) {
115 result = true;
116 }
117 }
118
119 int ilabrec = static_cast<int>(labrec);
120 int ilabbyt = static_cast<int>(labbyt);
121 int ilenbyt = static_cast<int>(lenbyt);
122 if( ilabbyt != ilabrec * ilenbyt ) {
123 result = false;
124 }
125 }
126
127 EXITFUNC;
128 return result;
129}
130
131
132bool SingleSpiderIO::is_valid_spider(const void *first_block)
133{
134 return SingleSpiderIO::is_valid(first_block);
135}
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
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
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)
Write a single SPIDER format header to an image.
Definition: sspiderio.cpp:51
bool is_valid_spider(const void *first_block)
check the data block to see if it represents valid stacked SPIDER image file header
Definition: sspiderio.cpp:132
int write_data(float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
Write data to an image.
Definition: sspiderio.cpp:63
static bool is_valid(const void *first_block)
Definition: sspiderio.cpp:71
SPIDER: (System for Processing Image Data from Electron microscopy and Related fields) is an image pr...
Definition: spiderio.h:72
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
@ SINGLE_IMAGE_HEADER
Definition: spiderio.h:193
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
SpiderHeader * first_h
Definition: spiderio.h:242
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
E2Exception class.
Definition: aligner.h:40