r236: *** empty log message ***
[ctsim.git] / include / array2dfile.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         array2dfile.h
5 **      Purpose:      2-dimension array file class
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: array2dfile.h,v 1.5 2000/12/04 02:52:57 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #ifndef ARRAY2DFILE_H
29 #define ARRAY2DFILE_H
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #endif
34
35 #include <sys/types.h>
36 #include <string.h>
37 #include <string>
38 #include <fstream>
39 #include <iostream>
40 #include <vector>
41 #include "ctsupport.h"
42 #include "fnetorderstream.h"
43 #include "array2d.h"
44
45 using namespace std;
46
47 class Array2dFileLabel
48 {
49 public:
50     static const int L_EMPTY = 0;
51     static const int L_HISTORY = 1;
52     static const int L_USER = 2;
53
54     Array2dFileLabel(); 
55
56     Array2dFileLabel(const char* const str, double ctime = 0.);
57
58     Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
59
60     ~Array2dFileLabel();
61
62     const string& getLabelString (void) const
63         { return m_strLabel; }
64
65     kfloat64 getCalcTime (void) const
66         { return m_calcTime; }
67
68     void setCalcTime (kfloat64 calcTime)
69         { m_calcTime = calcTime; }
70
71     void setLabelType (int labelType)
72         { m_labelType = labelType; }
73
74     int getLabelType (void) const
75         { return m_labelType; }
76
77     string& setLabelString (const char* const str)
78         { m_strLabel = str; return (m_strLabel); }
79
80     string& setLabelString (const string& str)
81         { m_strLabel = str; return (m_strLabel); }
82
83     void setDateTime (int year, int month, int day, int hour, int minute, int second);
84
85     void getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const;
86
87     const string& getDateString () const;
88
89     void print (ostream& os) const;
90
91     Array2dFileLabel (const Array2dFileLabel& rhs);
92
93     Array2dFileLabel& operator= (const Array2dFileLabel& rhs);
94
95 private:
96     void init (void);
97
98     kuint16 m_labelType;
99     kuint16 m_year;
100     kuint16 m_month;
101     kuint16 m_day;
102     kuint16 m_hour;
103     kuint16 m_minute;
104     kuint16 m_second;
105     string m_strLabel;
106     kfloat64 m_calcTime;
107
108     mutable string m_strDate;
109 };
110
111
112 class Array2dFile 
113 {
114 public:
115   static const int PIXEL_INVALID = 0;
116   static const int PIXEL_INT8 = 1;
117   static const int PIXEL_UINT8 = 2;
118   static const int PIXEL_INT16 = 3;
119   static const int PIXEL_UINT16 = 4;
120   static const int PIXEL_INT32 = 5;
121   static const int PIXEL_UINT32 = 6;
122   static const int PIXEL_FLOAT32 = 7;
123   static const int PIXEL_FLOAT64 = 8;
124
125   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
126   Array2dFile (void);
127   ~Array2dFile ();
128
129   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
130
131   void setArraySize (int nx, int ny);
132
133   unsigned int getNumLabels (void) const
134       { return m_labels.size(); }
135
136   const Array2dFileLabel& labelGet (int label_num) const;
137
138   void labelAdd (const Array2dFileLabel& label);
139
140   void labelAdd (const char* const m_strLabel, double calc_time=0.);
141
142   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
143
144   void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
145
146   void setPixelFormat (int type)
147       { m_pixelFormat = type; }
148
149   void setPixelSize (int size)
150       { m_pixelSize = size; }
151
152   kuint32 nx (void) const
153       { return m_nx; }
154
155   kuint32 ny (void) const
156       { return m_ny; }
157
158   void setAxisIncrement (double axisIncX, double axisIncY);
159
160   void setAxisExtent (double minX, double maxX, double minY, double maxY);
161
162   void getPixelValueRange (double& pvmin, double& pvmax) const;
163       
164   void doPixelOffsetScale (double offset, double scale);
165
166   void arrayDataClear (void);
167
168   bool fileRead (const char* const filename);
169
170   bool fileRead (const string& filename);
171
172   bool fileWrite (const char* const filename);
173
174   bool fileWrite (const string& filename);
175
176   const string& getFilename (void) const 
177       {  return m_filename; }
178
179   void printLabels (ostream& os) const;
180
181   typedef vector<Array2dFileLabel*>::iterator labelIterator;
182   typedef vector<Array2dFileLabel*>::const_iterator constLabelIterator;
183
184  protected:
185   typedef vector<Array2dFileLabel*> labelContainer;
186
187   static const kuint16 m_signature = ('I'*256+'F');
188   kuint16 m_headersize;
189   string  m_filename;
190
191   kuint16 m_pixelSize;
192   kuint16 m_pixelFormat;
193   kuint16 m_axisIncrementKnown;
194   kfloat64 m_axisIncrementX, m_axisIncrementY;
195   kuint16 m_axisExtentKnown;
196   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
197   kfloat64 m_offsetPV, m_scalePV;
198   kuint32 m_nx;
199   kuint32 m_ny;
200   kuint32 m_arraySize;
201   labelContainer m_labels;
202   kuint16 m_numFileLabels;
203   unsigned char** m_arrayData;
204
205 private:
206   void init (void);
207
208   bool headerRead (frnetorderstream& fs);
209
210   bool headerWrite (frnetorderstream& fs);
211
212   bool arrayDataRead (frnetorderstream& fs);
213
214   bool arrayDataWrite (frnetorderstream& fs);
215
216   bool labelsRead (frnetorderstream& fs);
217
218   bool labelsWrite (frnetorderstream& fs);
219
220   bool labelSeek (int label_num);
221
222   void allocArray (void);
223   
224   void freeArray (void);
225
226   Array2dFile (const Array2dFile& rhs);        // copy constructor
227   Array2dFile& operator= (const Array2dFile&); // assignment operator
228
229 };
230
231
232
233 #endif