omapio.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Grant Tang, 06/07/2011 (gtang@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #include "omapio.h"
00037 #include "portable_fileio.h"
00038 
00039 using namespace EMAN;
00040 
00041 OmapIO::OmapIO(const string & omapname, IOMode rw) :
00042                 filename(omapname), rw_mode(rw), omapfile(0),
00043                 is_big_endian(false), initialized(false), is_new_file(false)
00044 {
00045         memset(&omaph, 0, sizeof(OmapHeader));
00046         is_big_endian = ByteOrder::is_host_big_endian();
00047 }
00048 
00049 OmapIO::~OmapIO()
00050 {
00051         if (omapfile) {
00052                 fclose(omapfile);
00053                 omapfile = 0;
00054         }
00055 }
00056 
00057 void OmapIO::init()
00058 {
00059         ENTERFUNC;
00060 
00061         if (initialized) {
00062                 return;
00063         }
00064 
00065         initialized = true;
00066         omapfile = sfopen(filename, rw_mode, &is_new_file);
00067 
00068         char record[512];       //record size 512 bytes
00069 
00070         if (!is_new_file) {
00071                 if (fread(record, 512, 1, omapfile) != 1) {
00072                         throw ImageReadException(filename, "OMAP header");
00073                 }
00074 
00075                 for(int i=0; i<512; i++) {
00076                         if(!isprint(record[i])) {
00077                                 portable_fseek(omapfile, 0, SEEK_SET);
00078                                 break;
00079                         }
00080 
00081                         if(record[i] == '\0') break;
00082                 }
00083 
00084                 if (fread(&omaph, sizeof(OmapHeader), 1, omapfile) != 1) {
00085                         throw ImageReadException(filename, "OMAP header");
00086                 }
00087 
00088                 if (!is_valid(&omaph)) {
00089                         throw ImageReadException(filename, "invalid OMAP");
00090                 }
00091 
00092                 if(!ByteOrder::is_host_big_endian()) {  //omap first record is always  big endian
00093                         ByteOrder::swap_bytes((short *) &omaph, 256);   //each record 512 bytes, 256 short intergers
00094                 }
00095         }
00096 
00097         EXITFUNC;
00098 }
00099 
00100 int OmapIO::read_header(EMAN::Dict& dict, int, EMAN::Region const*, bool)
00101 {
00102         ENTERFUNC;
00103         init();
00104 
00105         dict["OMAP.xstart"] = omaph.xstart;
00106         dict["OMAP.ystart"] = omaph.ystart;
00107         dict["OMAP.zstart"] = omaph.zstart;
00108         dict["nx"] = omaph.nx;
00109         dict["ny"] = omaph.ny;
00110         dict["nz"] = omaph.nz;
00111         dict["apix_x"] = omaph.apix_x;
00112         dict["apix_y"] = omaph.apix_y;
00113         dict["apix_z"] = omaph.apix_z;
00114         dict["OMAP.header10"] = omaph.header10;
00115         dict["OMAP.header11"] = omaph.header11;
00116         dict["OMAP.header12"] = omaph.header12;
00117         dict["alpha"] = omaph.alpha;
00118         dict["beta"] = omaph.beta;
00119         dict["gamma"] = omaph.gamma;
00120         dict["OMAP.header16"] = omaph.header16;
00121         dict["OMAP.header17"] = omaph.header17;
00122         dict["OMAP.scale"] = omaph.scale;
00123         dict["OMAP.header19"] = omaph.header19;
00124 
00125         EXITFUNC;
00126         return 0;
00127 }
00128 
00129 int OmapIO::read_data(float *rdata, int, EMAN::Region const*, bool)
00130 {
00131         ENTERFUNC;
00132         std::cout << "file pointer location = " << ftell(omapfile) << std::endl;
00133 
00134         unsigned char * cdata = (unsigned char *) rdata;
00135         size_t size = (size_t)omaph.nx*omaph.ny*omaph.nz;
00136         if (fread(cdata, size, 1, omapfile) != 1) {
00137                 throw ImageReadException(filename, "OMAP data");
00138         }
00139 
00140         float density_factor = (float)omaph.header16/omaph.header19 + omaph.header17;
00141 
00142         std::cout << "density_factor = " << density_factor << std::endl;
00143 
00144         for (size_t i = 0; i < size; ++i) {
00145                 size_t j = size - 1 - i;
00146                 rdata[j] = static_cast < float >(cdata[j]) * density_factor;
00147         }
00148 
00149         EXITFUNC;
00150         return 0;
00151 }
00152 
00153 int OmapIO::write_header(EMAN::Dict const&, int, EMAN::Region const*, EMAN::EMUtil::EMDataType, bool)
00154 {
00155         ENTERFUNC;
00156 
00157         EXITFUNC;
00158         return 0;
00159 }
00160 
00161 int OmapIO::write_data(float*, int, EMAN::Region const*, EMAN::EMUtil::EMDataType, bool)
00162 {
00163         ENTERFUNC;
00164 
00165         EXITFUNC;
00166         return 0;
00167 }
00168 
00169 bool OmapIO::is_valid(const void *first_block, off_t file_size)
00170 {
00171         ENTERFUNC;
00172 
00173         if (!first_block) {
00174                 return false;
00175         }
00176 
00177         const short *data = static_cast < const short *>(first_block);
00178         short xstart = data[0];
00179         short ystart = data[1];
00180         short zstart = data[2];
00181         short nx = data[3];
00182         short ny = data[4];
00183         short nz = data[5];
00184         short const_value = data[18];
00185 
00186         if(!ByteOrder::is_host_big_endian()) {
00187                 ByteOrder::swap_bytes(&xstart);
00188                 ByteOrder::swap_bytes(&ystart);
00189                 ByteOrder::swap_bytes(&zstart);
00190                 ByteOrder::swap_bytes(&nx);
00191                 ByteOrder::swap_bytes(&ny);
00192                 ByteOrder::swap_bytes(&nz);
00193                 ByteOrder::swap_bytes(&const_value);
00194         }
00195 
00196 //      std::cout << "const_value = " << const_value
00197 //                      << ", xstart = " << xstart
00198 //                      << ", ystart = " << ystart
00199 //                      << ", zstart = " << zstart
00200 //                      << ", nx = " << nx
00201 //                      << ", ny = " << ny
00202 //                      << ", nz = " << nz
00203 //                      << std::endl;
00204 
00205         if(const_value != 100) return false;
00206         if(nx<=0 || ny<=0 || nz<=0 || nx>10000 || ny>10000 || nz>10000) return false;
00207 
00208         EXITFUNC;
00209         return true;
00210 }
00211 
00212 bool OmapIO::is_image_big_endian()
00213 {
00214         return true;
00215 }
00216 
00217 bool OmapIO::is_complex_mode()
00218 {
00219         return false;
00220 }
00221 
00222 void OmapIO::flush()
00223 {
00224         fflush(omapfile);
00225 }

Generated on Thu Nov 17 12:42:59 2011 for EMAN2 by  doxygen 1.4.7