EMAN2
dm3io.h
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@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 #ifndef __dm3io_h__
00037 #define __dm3io_h__
00038 
00039 #include "imageio.h"
00040 
00041 using std::vector;
00042 using std::map;
00043 
00044 namespace EMAN
00045 {
00046         namespace Gatan
00047         {
00048                 class TagTable
00049                 {
00050                   public:
00051                         TagTable();
00052                         ~TagTable();
00053 
00054                         void add(const string & name, const string & value);
00055                         void add_data(char *data);
00056 
00057                         string get_string(const string & name);
00058                         int get_int(const string & name);
00059                         float get_float(const string & name);
00060                         double get_double(const string & name);
00061 
00062                         int get_xsize() const;
00063                         int get_ysize() const;
00064                         int get_datatype() const;
00065                         char *get_data() const;
00066 
00067                         void dump() const;
00068 
00069                         template < class T > void become_host_endian(T * data, int n = 1) {
00070                                 if (is_big_endian != ByteOrder::is_host_big_endian()) {
00071                                         ByteOrder::swap_bytes(data, n);
00072                                 }
00073                         }
00074 
00075                         void set_endian(bool big_endian)
00076                         {
00077                                 is_big_endian = big_endian;
00078                         }
00079 
00080                   private:
00081                         static const char *IMAGE_WIDTH_TAG;
00082                         static const char *IMAGE_HEIGHT_TAG;
00083                         static const char *IMAGE_DATATYPE_TAG;
00084                         static const char *IMAGE_THUMB_INDEX_TAG;
00085                         void set_thumb_index(int i);
00086 
00087                   private:
00088                         int img_index;
00089                         bool is_big_endian;
00090                         std::map < string, string > tags;
00091                         vector < int >x_list;
00092                         vector < int >y_list;
00093                         vector < int >datatype_list;
00094                         vector < char *>data_list;
00095                 };
00096 
00097                 class TagData
00098                 {
00099                   public:
00100                         enum Type
00101                         {
00102                                 UNKNOWN = 0,
00103                                 SHORT = 2,
00104                                 INT = 3,
00105                                 USHORT = 4,
00106                                 UINT = 5,
00107                                 FLOAT = 6,
00108                                 DOUBLE = 7,
00109                                 BOOLEAN = 8,
00110                                 CHAR = 9,
00111                                 OCTET = 10,
00112                                 STRUCT = 15,
00113                                 STRING = 18,
00114                                 ARRAY = 20
00115                         };
00116 
00117                         TagData(FILE * data_file, TagTable * tagtable, const string & tagname);
00118                         ~TagData();
00119 
00120                         int read(bool nodata = false);
00121 
00122                   private:
00123                         size_t typesize() const;
00124                         size_t typesize(int type) const;
00125                         int read_any(bool nodata = false);
00126 
00127                         vector < int >read_array_types();
00128                         int read_array_data(vector < int >item_types, bool nodata = false);
00129                         vector < int >read_struct_types();
00130                         string read_native(bool is_value_stored);
00131                         string read_string(int size);
00132 
00133                   private:
00134                         FILE * in;
00135                         TagTable *tagtable;
00136                         string name;
00137                         Type tag_type;
00138                 };
00139 
00140 
00141                 class TagGroup
00142                 {
00143                   public:
00144                         TagGroup(FILE * data_file, TagTable * tagtable, const string & groupname);
00145                         ~TagGroup();
00146 
00147                         int read(bool nodata = false);
00148                         string get_name() const;
00149                         int get_entry_id();
00150 
00151                   private:
00152                         FILE * in;
00153                         TagTable *tagtable;
00154                         string name;
00155                         int entry_id;
00156                 };
00157 
00158 
00159                 class TagEntry
00160                 {
00161                   public:
00162                         enum EntryType
00163                         {
00164                                 GROUP_TAG = 20,
00165                                 DATA_TAG = 21
00166                         };
00167 
00168                         TagEntry(FILE * data_file, TagTable * tagtable, TagGroup * parent_group);
00169                         ~TagEntry();
00170 
00171                         int read(bool nodata = false);
00172 
00173                   private:
00174                         FILE * in;
00175                         TagTable *tagtable;
00176                         TagGroup *parent_group;
00177                         string name;
00178                 };
00179 
00180 
00181                 class DataType
00182                 {
00183                   public:
00184                         enum GatanDataType
00185                         {
00186                                 NULL_DATA,
00187                                 SIGNED_INT16_DATA,
00188                                 REAL4_DATA,
00189                                 COMPLEX8_DATA,
00190                                 OBSELETE_DATA,
00191                                 PACKED_DATA,
00192                                 UNSIGNED_INT8_DATA,
00193                                 SIGNED_INT32_DATA,
00194                                 RGB_DATA,
00195                                 SIGNED_INT8_DATA,
00196                                 UNSIGNED_INT16_DATA,
00197                                 UNSIGNED_INT32_DATA,
00198                                 REAL8_DATA,
00199                                 COMPLEX16_DATA,
00200                                 BINARY_DATA,
00201                                 RGB_UINT8_0_DATA,
00202                                 RGB_UINT8_1_DATA,
00203                                 RGB_UINT16_DATA,
00204                                 RGB_FLOAT32_DATA,
00205                                 RGB_FLOAT64_DATA,
00206                                 RGBA_UINT8_0_DATA,
00207                                 RGBA_UINT8_1_DATA,
00208                                 RGBA_UINT8_2_DATA,
00209                                 RGBA_UINT8_3_DATA,
00210                                 RGBA_UINT16_DATA,
00211                                 RGBA_FLOAT32_DATA,
00212                                 RGBA_FLOAT64_DATA,
00213                                 POINT2_SINT16_0_DATA,
00214                                 POINT2_SINT16_1_DATA,
00215                                 POINT2_SINT32_0_DATA,
00216                                 POINT2_FLOAT32_0_DATA,
00217                                 RECT_SINT16_1_DATA,
00218                                 RECT_SINT32_1_DATA,
00219                                 RECT_FLOAT32_1_DATA,
00220                                 RECT_FLOAT32_0_DATA,
00221                                 SIGNED_INT64_DATA,
00222                                 UNSIGNED_INT64_DATA,
00223                                 LAST_DATA
00224                         };
00225                 };
00226 
00227                 int to_em_datatype(int gatan_datatype);
00228                 const char *to_str(Gatan::TagData::Type type);
00229                 const char *to_str(Gatan::TagEntry::EntryType type);
00230                 const char *to_str(Gatan::DataType::GatanDataType type);
00231         }
00232 
00246         class DM3IO:public ImageIO
00247         {
00248           public:
00249                 explicit DM3IO(const string & filename, IOMode rw_mode = READ_ONLY);
00250                 ~DM3IO();
00251 
00252                 DEFINE_IMAGEIO_FUNC;
00253                 static bool is_valid(const void *first_block);
00254 
00255           private:
00256                 enum
00257                 { NUM_ID_INT = 3 };
00258 
00259           private:
00260                 string filename;
00261                 IOMode rw_mode;
00262                 FILE *dm3file;
00263                 bool is_big_endian;
00264                 bool initialized;
00265                 Gatan::TagTable * tagtable;
00266         };
00267 
00268 }
00269 
00270 
00271 
00272 
00273 #endif  //__dm3io_h__