r99: *** empty log message ***
[ctsim.git] / libctsim / imagefile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         imagefile.cpp
5 **      Purpose:      Imagefile classes
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: imagefile.cpp,v 1.1 2000/06/19 02:59:34 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 #include "ct.h"
29
30
31 ///////////////////////////////////////////////////////////////////////////
32 // CLASS IMPLEMENTATION
33 //
34 //     Name: Array2dFileLabel
35 //     Purpose: Labels for Array2dFiles
36 ///////////////////////////////////////////////////////////////////////////
37
38 void
39 Array2dFileLabel::init (void)
40 {
41     m_calcTime = 0;
42     m_labelType = L_EMPTY;
43     TIMEDATE td;
44     td_get_tmdt (&td);
45     m_year = td.d.year;
46     m_month = td.d.month;
47     m_day = td.d.date;
48     m_hour = td.t.hour;
49     m_minute = td.t.minute;
50     m_second = td.t.second;
51 }
52
53 Array2dFileLabel::Array2dFileLabel() 
54 {
55     init();
56 }
57
58 Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime = 0.)
59   : m_strLabel (str)
60 {
61     init();
62
63     m_labelType = L_USER;
64     m_calcTime = ctime;
65 }
66
67 Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime = 0.)
68   :  m_strLabel (str)
69 {
70     init();
71
72     m_labelType = type;
73     m_calcTime = ctime;
74 }
75
76 Array2dFileLabel::~Array2dFileLabel()
77 {
78 }
79
80 const string& 
81 Array2dFileLabel::getDateString (void) const
82 {
83   ostringstream oss;
84   oss <<  m_month <<"/"<< m_day <<"/"<< m_year << " " << m_hour <<":"<<  m_minute <<":"<< m_second;
85   m_strDate = oss.str();
86   return m_strDate;
87 }
88
89
90 /* FILE
91  *   image.c                            Routines for managing images
92  *
93  * PROGRAMMER:  Kevin Rosenberg
94  * DATE:        Aug 1984
95  *
96  * FUNCTION
97  *      Provides a set of routines for handling image files
98  */
99
100
101 void 
102 image_filter_response (ImageFile& im, const DomainType domain, double bw, const FilterType filt_type, double filt_param, const int opt_trace)
103 {
104   int hx = im.nx() / 2;
105   int hy = im.ny() / 2;
106   ImageFileArray v = im.getArray();
107
108   for (int i = -hx; i <= hx; i++) {
109     for (int j = -hy; j <= hy; j++) {
110       double r = sqrt(i * i + j * j);
111       
112       if (domain == D_SPATIAL)
113         v[i+hx][j+hy] = filter_spatial_response_analytic (filt_type, r, bw, filt_param);
114       else
115         v[i+hx][j+hy] = filter_frequency_response (filt_type, r, bw, filt_param);
116       if (opt_trace >= TRACE_PHM)
117         printf ("r=%8.4f, v=%8.4f\n", r, v[i+hx][j+hy]);
118     }
119   }
120 }
121
122 int
123 image_display (const ImageFile& im)
124 {
125     ImageFileValue pmin, pmax;
126
127     im.getPixelValueRange (pmin, pmax);
128
129     return (image_display_scale (im, 1, pmin, pmax));
130 }
131
132 int image_display_scale (const ImageFile& im, const int scale, const double pmin, const double pmax)
133 {
134     int grayscale[256];
135     int nx = im.nx();
136     int ny = im.ny();
137     ImageFileArray v = im.getArray();
138
139 #if HAVE_G2_H
140     int* pens = new int [nx * ny * scale * scale ];
141
142     double view_scale = 255 / (pmax - pmin);
143     int id_X11 = g2_open_X11 (nx * scale, ny * scale);
144
145     for (int i = 0; i < 256; i++) {
146         double cval = i / 255.;
147         grayscale[i] = g2_ink (id_X11, cval, cval, cval);
148     }
149
150     for (int i= 0, iy = ny - 1; iy >= 0; iy--) {
151         for (int ix = 0; ix < nx; ix++) {
152             int cval = (int) ((v[ix][iy] - pmin) * view_scale);
153             if (cval < 0)  
154               cval = 0;
155             else if (cval > 255) 
156               cval = 255;
157             pens[i++] = grayscale[cval];
158         }
159     }
160
161     g2_image (id_X11, 0., 0., nx, ny, pens);
162
163     delete [] pens;
164     return (id_X11);
165
166 #endif
167 }
168
169
170
171