00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMPEM_SPIDER_READER_WRITER_H
00010 #define IMPEM_SPIDER_READER_WRITER_H
00011
00012 #include "IMP/em/em_config.h"
00013 #include "IMP/em/ImageHeader.h"
00014 #include "IMP/em/ImageReaderWriter.h"
00015 #include "IMP/em/header_converters.h"
00016 #include "IMP/em/MapReaderWriter.h"
00017 #include "IMP/em/DensityHeader.h"
00018 #include <IMP/algebra/Matrix2D.h>
00019 #include <IMP/algebra/utility.h>
00020 #include <IMP/algebra/endian.h>
00021 #include <IMP/exception.h>
00022 #include <typeinfo>
00023 #include <complex>
00024 #include <string>
00025 #include <cstdio>
00026 #include <iostream>
00027 #include <fstream>
00028 #include <cstring>
00029
00030 IMPEM_BEGIN_NAMESPACE
00031
00032
00033
00034
00035
00036
00037
00038 template <typename T>
00039 class SpiderImageReaderWriter: public ImageReaderWriter<T>
00040 {
00041 public:
00042 String filename_;
00043 bool skip_type_check_;
00044 bool force_reversed_;
00045 bool skip_extra_checkings_;
00046
00047
00048
00049
00050
00051
00052 SpiderImageReaderWriter() {
00053 skip_type_check_=false;
00054 force_reversed_=false;
00055 skip_extra_checkings_=false;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 SpiderImageReaderWriter(String filename,bool skip_type_check,
00068 bool force_reversed,bool skip_extra_checkings) {
00069 filename_=filename;
00070 skip_type_check_=skip_type_check;
00071 force_reversed_=force_reversed;
00072 skip_extra_checkings_=skip_extra_checkings;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 void read(String filename, ImageHeader& header,
00084 algebra::Matrix2D<T>& data) {
00085 #ifdef IMP_DEBUG_SPIDER
00086 std::cout << "reading with SpiderImageReaderWriter" << std::endl;
00087 #endif
00088 std::ifstream in;
00089 in.open(filename.c_str(), std::ios::in | std::ios::binary);
00090
00091
00092 header.read(in,skip_type_check_,force_reversed_,skip_extra_checkings_);
00093 #ifdef IMP_DEBUG_SPIDER
00094 std::cout << header << std::endl;
00095 #endif
00096
00097 data.resize((int)header.get_number_of_rows(),
00098 (int)header.get_number_of_columns());
00099 data.read_binary(in,force_reversed_ ^ algebra::get_is_big_endian());
00100 in.close();
00101 }
00102
00103 void read_from_floats(String filename, ImageHeader& header,
00104 algebra::Matrix2D<T>& data) {
00105 std::ifstream in;
00106 in.open(filename.c_str(), std::ios::in | std::ios::binary);
00107 IMP_USAGE_CHECK(!in.fail(),
00108 "SpiderReaderWriter::read_from_floats: The file "+
00109 filename+" could be found.");
00110
00111 header.read(in,skip_type_check_,force_reversed_,skip_extra_checkings_);
00112 #ifdef IMP_DEBUG_SPIDER
00113 std::cout << header << std::endl;
00114 #endif
00115
00116 data.resize((int)header.get_number_of_rows(),
00117 (int)header.get_number_of_columns());
00118
00119 float aux;
00120 for (unsigned long i=0;i<data.num_elements();i++) {
00121 if (!(force_reversed_ ^ algebra::get_is_big_endian())) {
00122 in.read(reinterpret_cast< char* >(&aux), sizeof(float));
00123 } else {
00124 algebra::reversed_read(reinterpret_cast< char* >(&aux),
00125 sizeof(float),1,in,true);
00126 }
00127 data.data()[i] = (T)aux;
00128 }
00129 in.close();
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 void write(String filename, ImageHeader& header,
00139 algebra::Matrix2D<T>& data) {
00140 std::ofstream out;
00141 out.open(filename.c_str(), std::ios::out | std::ios::binary);
00142
00143 header.write(out, force_reversed_ ^ algebra::get_is_big_endian());
00144 data.write_binary(out,force_reversed_ ^ algebra::get_is_big_endian());
00145 out.close();
00146 }
00147
00148
00149 void write_to_floats(String filename, ImageHeader& header,
00150 algebra::Matrix2D<T>& data) {
00151 std::ofstream out;
00152 out.open(filename.c_str(), std::ios::out | std::ios::binary);
00153
00154 header.write(out, force_reversed_ ^ algebra::get_is_big_endian());
00155
00156 float aux;
00157 for (unsigned long i=0;i<data.num_elements();i++) {
00158 aux = (float)data.data()[i];
00159 if (!(force_reversed_ ^ algebra::get_is_big_endian())) {
00160 out.write(reinterpret_cast< char* >(&aux), sizeof(float));
00161 } else {
00162 algebra::reversed_write(reinterpret_cast< char* >(&aux),
00163 sizeof(float),1,out,true);
00164 }
00165 }
00166
00167 out.close();
00168 }
00169
00170 };
00171
00172
00173
00174
00175
00176
00177 class IMPEMEXPORT SpiderMapReaderWriter : public MapReaderWriter {
00178 public:
00179 String filename_;
00180 bool skip_type_check_;
00181 bool force_reversed_;
00182 bool skip_extra_checkings_;
00183
00184
00185
00186 SpiderMapReaderWriter() {
00187 skip_type_check_=false;
00188 force_reversed_=false;
00189 skip_extra_checkings_=false;
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 SpiderMapReaderWriter(String filename,bool skip_type_check,
00202 bool force_reversed,bool skip_extra_checkings) {
00203 filename_=filename;
00204 skip_type_check_=skip_type_check;
00205 force_reversed_=force_reversed;
00206 skip_extra_checkings_=skip_extra_checkings;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216 void Read(const char *filename, float **data, DensityHeader &header);
00217
00218
00219
00220
00221
00222
00223
00224
00225 void Write(const char *filename,
00226 const float *data,const DensityHeader &header);
00227 };
00228
00229 IMPEM_END_NAMESPACE
00230
00231 #endif