r121: *** 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.1 2000/06/26 21:15:24 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 #include <sys/types.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <string>
35 #include <fstream>
36 #include <iostream>
37 #include <vector>
38 #include "ctsupport.h"
39 #include "fnetorderstream.h"
40 #include "array2d.h"
41
42 using namespace std;
43
44 class Array2dFileLabel
45 {
46 public:
47     static const int L_EMPTY = 0;
48     static const int L_HISTORY = 1;
49     static const int L_USER = 2;
50
51     Array2dFileLabel(); 
52
53     Array2dFileLabel(const char* const str, double ctime = 0.);
54
55     Array2dFileLabel(const int type, const char* const str, double ctime = 0.);
56
57     ~Array2dFileLabel();
58
59     const string& getLabelString (void) const
60         { return m_strLabel; }
61
62     kfloat64 getCalcTime (void) const
63         { return m_calcTime; }
64
65     int getLabelType (void) const
66         { return m_labelType; }
67
68     string& setLabelString (const char* const str)
69         { m_strLabel = str; return (m_strLabel); }
70
71     string& setLabelString (const string& str)
72         { m_strLabel = str; return (m_strLabel); }
73
74     void setDateTime (int year, int month, int day, int hour, int minute, int second);
75
76     void getDateTime (int& year, int& month, int& day, int& hour, int& minute, int& second) const;
77
78     const string& getDateString () const;
79
80     // Array2dFileLabel (const Array2dFileLabel&);
81
82     // Array2dFileLabel& operator= (const Array2dFileLabel&);
83
84 private:
85     void init (void);
86
87     kuint16 m_labelType;
88     kuint16 m_year;
89     kuint16 m_month;
90     kuint16 m_day;
91     kuint16 m_hour;
92     kuint16 m_minute;
93     kuint16 m_second;
94     string m_strLabel;
95     kfloat64 m_calcTime;
96
97     mutable string m_strDate;
98 };
99
100
101 class Array2dFile 
102 {
103 public:
104   static const int PIXEL_INVALID = 0;
105   static const int PIXEL_INT8 = 1;
106   static const int PIXEL_UINT8 = 2;
107   static const int PIXEL_INT16 = 3;
108   static const int PIXEL_UINT16 = 4;
109   static const int PIXEL_INT32 = 5;
110   static const int PIXEL_UINT32 = 6;
111   static const int PIXEL_FLOAT32 = 7;
112   static const int PIXEL_FLOAT64 = 8;
113
114   Array2dFile (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
115   Array2dFile (void);
116   ~Array2dFile ();
117
118   void setArraySize (int nx, int ny, int pixelSize, int pixelFormat = PIXEL_INVALID);
119
120   void setArraySize (int nx, int ny);
121
122   unsigned int getNumLabels (void) const
123       { return m_labels.size(); }
124
125   const Array2dFileLabel& labelGet (int label_num) const;
126
127   void labelAdd (const Array2dFileLabel& label);
128
129   void labelAdd (const char* const m_strLabel, double calc_time=0.);
130
131   void labelAdd (int type, const char* const m_strLabel, double calc_time=0.);
132
133   void labelsCopy (Array2dFile& file, const char* const idStr = NULL);
134
135   void setPixelFormat (int type)
136       { m_pixelFormat = type; }
137
138   void setPixelSize (int size)
139       { m_pixelSize = size; }
140
141   kuint32 nx (void) const
142       { return m_nx; }
143
144   kuint32 ny (void) const
145       { return m_ny; }
146
147   void setAxisIncrement (double axisIncX, double axisIncY);
148
149   void setAxisExtent (double minX, double maxX, double minY, double maxY);
150
151   void getPixelValueRange (double& pvmin, double& pvmax) const;
152       
153   void doPixelOffsetScale (double offset, double scale);
154
155   void arrayDataClear (void);
156
157   bool fileRead (const char* const filename);
158
159   bool fileWrite (const char* const filename);
160
161   const string& getFilename (void) const 
162       {  return m_filename; }
163
164   void printLabels (ostream& os) const;
165
166   typedef vector<Array2dFileLabel*>::iterator labelIterator;
167   typedef vector<Array2dFileLabel*>::const_iterator constLabelIterator;
168
169  protected:
170   typedef vector<Array2dFileLabel*> labelContainer;
171
172   static const kuint16 m_signature = ('I'*256+'F');
173   kuint16 m_headersize;
174   string  m_filename;
175
176   kuint16 m_pixelSize;
177   kuint16 m_pixelFormat;
178   kuint16 m_axisIncrementKnown;
179   kfloat64 m_axisIncrementX, m_axisIncrementY;
180   kuint16 m_axisExtentKnown;
181   kfloat64 m_minX, m_maxX, m_minY, m_maxY;
182   kfloat64 m_offsetPV, m_scalePV;
183   kuint32 m_nx;
184   kuint32 m_ny;
185   kuint32 m_arraySize;
186   labelContainer m_labels;
187   kuint16 m_numFileLabels;
188   unsigned char** m_arrayData;
189
190 private:
191   void init (void);
192
193   bool headerRead (frnetorderstream& fs);
194
195   bool headerWrite (frnetorderstream& fs);
196
197   bool arrayDataRead (frnetorderstream& fs);
198
199   bool arrayDataWrite (frnetorderstream& fs);
200
201   bool labelsRead (frnetorderstream& fs);
202
203   bool labelsWrite (frnetorderstream& fs);
204
205   bool labelSeek (int label_num);
206
207   void allocArray (void);
208   
209   void freeArray (void);
210
211   Array2dFile (const Array2dFile& rhs);        // copy constructor
212   Array2dFile& operator= (const Array2dFile&); // assignment operator
213
214 };
215
216
217
218 #endif