EMAN2
dm4io.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 __dm4io_h__
00037 #define __dm4io_h__
00038 
00039 #include "imageio.h"
00040 
00041 namespace EMAN
00042 {
00043         namespace GatanDM4
00044         {
00045                 class TagTable
00046                 {
00047                   public:
00048                         TagTable();
00049                         ~TagTable();
00050 
00051                         void add(const string & name, const string & value);
00052                         void add_data(char *data);
00053 
00054                         string get_string(const string & name);
00055                         int get_int(const string & name);
00056                         float get_float(const string & name);
00057                         double get_double(const string & name);
00058 
00059                         int get_image_counted() const;
00060                         int get_xsize() const;
00061                         int get_ysize() const;
00062                         int get_datatype() const;
00063                         char *get_data() const;
00064                         int get_num_images_found() const;
00065                         void set_num_images_found(int num_found);
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_NIMG_TAG;
00084                         static const char *IMAGE_DATATYPE_TAG;
00085                         static const char *IMAGE_THUMB_INDEX_TAG;
00086                         void set_thumb_index(int i);
00087 
00088                   private:
00089                         int img_counted;
00090                         int img_index;
00091                         int num_images_found;
00092                         bool is_big_endian;
00093                         std::map < string, string > tags;
00094                         vector < int >x_list;
00095                         vector < int >y_list;
00096                         vector < int >datatype_list;
00097                         vector < char *>data_list;
00098                 };
00099 
00100                 class TagData
00101                 {
00102                   public:
00103                         enum Type
00104                         {
00105                                 UNKNOWN = 0,
00106                                 SHORT = 2,
00107                                 INT = 3,
00108                                 USHORT = 4,
00109                                 UINT = 5,
00110                                 FLOAT = 6,
00111                                 DOUBLE = 7,
00112                                 BOOLEAN = 8,
00113                                 CHAR = 9,
00114                                 OCTET = 10,
00115                                 OCTEU = 11,
00116                                 OCTEV = 12,
00117                                 STRUCT = 15,
00118                                 STRING = 18,
00119                                 ARRAY = 20
00120                         };
00121 
00122                         TagData(FILE * data_file, TagTable * tagtable, const string & tagname);
00123                         ~TagData();
00124 
00125                         int read_tag_data(bool nodata = false, int image_index = 0, int num_images = 1);
00126 
00127                   private:
00128                         size_t typesize() const;
00129                         size_t typesize(int type) const;
00130                         int read_any(bool nodata = false, int image_index = 0, int num_images = 1);
00131 
00132                         vector < int >read_array_types();
00133                         int read_array_data(vector < int >item_types, bool nodata = false, int image_index = 0, int num_images = 1);
00134                         vector < int >read_struct_types();
00135                         string read_native(bool is_value_stored);
00136                         string read_string(int size);
00137 
00138                   private:
00139                         FILE * in;
00140                         TagTable *tagtable;
00141                         string name;
00142                         long long tag_type;
00143                 };
00144 
00145                 class TagGroup
00146                 {
00147                   public:
00148                         TagGroup(FILE * data_file, TagTable * tagtable, const string & groupname);
00149                         ~TagGroup();
00150 
00151                         int read_tag_group(bool nodata = false, int image_index = 0, int num_images = 1);
00152                         string get_name() const;
00153                         int get_entry_id();
00154 
00155                   private:
00156                         FILE * in;
00157                         TagTable *tagtable;
00158                         string name;
00159                         int entry_id;
00160                 };
00161 
00162                 class TagEntry
00163                 {
00164                   public:
00165                         enum EntryType
00166                         {
00167                                 GROUP_TAG = 20,
00168                                 DATA_TAG = 21
00169                         };
00170 
00171                         TagEntry(FILE * data_file, TagTable * tagtable, TagGroup * parent_group);
00172                         ~TagEntry();
00173 
00174                         int read_tag_entry(bool nodata = false, int image_index = 0, int num_images = 1);
00175 
00176                   private:
00177                         FILE * in;
00178                         TagTable *tagtable;
00179                         TagGroup *parent_group;
00180                         string name;
00181                 };
00182 
00183                 class DataType
00184                 {
00185                   public:
00186                         enum GatanDataType
00187                         {
00188                                 NULL_DATA,
00189                                 SIGNED_INT16_DATA,
00190                                 REAL4_DATA,
00191                                 COMPLEX8_DATA,
00192                                 OBSELETE_DATA,
00193                                 PACKED_DATA,
00194                                 UNSIGNED_INT8_DATA,
00195                                 SIGNED_INT32_DATA,
00196                                 RGB_DATA,
00197                                 SIGNED_INT8_DATA,
00198                                 UNSIGNED_INT16_DATA,
00199                                 UNSIGNED_INT32_DATA,
00200                                 REAL8_DATA,
00201                                 COMPLEX16_DATA,
00202                                 BINARY_DATA,
00203                                 RGB_UINT8_0_DATA,
00204                                 RGB_UINT8_1_DATA,
00205                                 RGB_UINT16_DATA,
00206                                 RGB_FLOAT32_DATA,
00207                                 RGB_FLOAT64_DATA,
00208                                 RGBA_UINT8_0_DATA,
00209                                 RGBA_UINT8_1_DATA,
00210                                 RGBA_UINT8_2_DATA,
00211                                 RGBA_UINT8_3_DATA,
00212                                 RGBA_UINT16_DATA,
00213                                 RGBA_FLOAT32_DATA,
00214                                 RGBA_FLOAT64_DATA,
00215                                 POINT2_SINT16_0_DATA,
00216                                 POINT2_SINT16_1_DATA,
00217                                 POINT2_SINT32_0_DATA,
00218                                 POINT2_FLOAT32_0_DATA,
00219                                 RECT_SINT16_1_DATA,
00220                                 RECT_SINT32_1_DATA,
00221                                 RECT_FLOAT32_1_DATA,
00222                                 RECT_FLOAT32_0_DATA,
00223                                 SIGNED_INT64_DATA,
00224                                 UNSIGNED_INT64_DATA,
00225                                 LAST_DATA
00226                         };
00227                 };
00228 
00229                 int to_em_datatype(int gatan_datatype);
00230                 const char *to_str(GatanDM4::TagData::Type type);
00231                 const char *to_str(GatanDM4::TagEntry::EntryType type);
00232                 const char *to_str(GatanDM4::DataType::GatanDataType type);
00233 
00234         }
00235 
00253         class DM4IO : public ImageIO
00254         {
00255           public:
00256                 explicit DM4IO(const string & filename, IOMode rw_mode = READ_ONLY);
00257                 ~DM4IO();
00258 
00259                 DEFINE_IMAGEIO_FUNC;
00260                 static bool is_valid(const void *first_block);
00261                 int get_nimg();
00262 
00263           private:
00264                 enum { NUM_ID_INT = 4 };        //actually its int+long+int=16 bytes
00265 
00266                 string filename;
00267                 IOMode rw_mode;
00268                 FILE *dm4file;
00269                 bool is_big_endian;
00270                 bool initialized;
00271                 GatanDM4::TagTable * tagtable;
00272         };
00273 }
00274 
00275 #endif  //__dm4io_h__