File: spectraBox.cxx

package info (click to toggle)
imview 1.1.8-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,052 kB
  • ctags: 3,797
  • sloc: cpp: 29,842; sh: 2,620; ansic: 1,835; makefile: 756; exp: 112; python: 88
file content (322 lines) | stat: -rw-r--r-- 7,605 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * $Id: spectraBox.cxx,v 4.0 2003/04/28 14:40:03 hut66au Exp $
 *
 * Imview, the portable image analysis application
 * http://www.cmis.csiro.au/Hugues.Talbot/imview
 * ----------------------------------------------------------
 *
 *  Imview is an attempt to provide an image display application
 *  suitable for professional image analysis. It was started in
 *  1997 and is mostly the result of the efforts of Hugues Talbot,
 *  Image Analysis Project, CSIRO Mathematical and Information
 *  Sciences, with help from others (see the CREDITS files for
 *  more information)
 *
 *  Imview is Copyrighted (C) 1997-2001 by Hugues Talbot and was
 *  supported in parts by the Australian Commonwealth Science and 
 *  Industry Research Organisation. Please see the COPYRIGHT file 
 *  for full details. Imview also includes the contributions of 
 *  many others. Please see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 * */

/*------------------------------------------------------------------------
 *
 * The code for the spectrum display panel
 *
 * Hugues Talbot	 1 Aug 1998
 *      
 *-----------------------------------------------------------------------*/

#include <string.h>
#include "imview.hxx"
#include "spectraBox.hxx"
#include "saveSpect.hxx"
#include "printSpect.hxx"

extern savespect        *saveSpectPanel;
extern printspect       *printSpectPanel;

void mySpectraBox::setSpectrum(double *theSpectrum, int nbval)
{
    if (theSpectrum != 0) {
	if (spectrum) delete[] spectrum;
	spectrum = new double[nbval];
	memcpy(spectrum, theSpectrum, nbval*sizeof(double));
	nbSpectrumValues = nbval;
	recomputeLimits();
    } else {
	if (spectrum) delete[] spectrum;
	spectrum = 0;
	nbSpectrumValues = 0;
    }

    return;
}

void mySpectraBox::recomputeLimits(void)
{
    if (spectrum) {
	if (!absolute)
	    mins = maxs = spectrum[0];
	// else they have already been set to 0
	for (int i = 1 ; i < nbSpectrumValues ; i++) {
	    if (spectrum[i] > maxs) maxs = spectrum[i];
	    if (spectrum[i] < mins) mins = spectrum[i];
	}
	// special case of the char values.
	if (absolute && (mins >= 0.0) && (maxs <= 255.0)) {
	    mins = 0.0;
	    maxs = 255.0;
	}
	// what if they are both equal?
	if (maxs == mins)
	    maxs = 1.0;
	if (theSpectra) {
	    theSpectra->setAxisBoxesLimits(0, nbSpectrumValues,
					   mins, maxs);
	}
    }

    return;
}

int mySpectraBox::handle(int event)
{
    int retval = 0;
    int button;

    if (spectrum) {
	button = Fl::event_button();
	switch (event) {
	  case FL_PUSH:
	    handleButtonPushed();
	    retval = 1;
	    break;

	  case FL_DRAG:
	    handleButtonDragged();
	    retval = 1;
	    break;

	  case FL_RELEASE:
	    handleButtonReleased();
	    retval = 1;
	    break;

	  case FL_LEAVE:
	    // Return to normal cursor
	    fl_cursor(FL_CURSOR_DEFAULT);
	    retval = 1;
	    break;
	    
	  default:
	    retval = 0;
	    break;
	}
    }
    
    return retval;
}

void mySpectraBox::handleButtonPushed()
{

    fl_cursor((Fl_Cursor)MY_CURSOR_VLINE);
    handleButtonDragged();
    
    return;
}

void mySpectraBox::handleButtonDragged()
{
    int index;
    int xx = Fl::event_x();
    int realwidth = w() - WIDTHMARGIN;
    
    index = (int)(((double)xx-x()-WIDTHMARGIN/2)*(nbSpectrumValues-1)/realwidth);
    if ((index >= 0) && (index < nbSpectrumValues) && theSpectra) {
	theSpectra->setXValue(index);
	theSpectra->setYValue(spectrum[index]);
    } 
    return;
}

void mySpectraBox::handleButtonReleased()
{
    fl_cursor(FL_CURSOR_DEFAULT);

    return;
}

void mySpectraBox::draw()
{
    // superclass draw:
    draw_box();
    draw_label();

    if (spectrum != 0) {
	int xa, ya, xb, yb;
	int realwidth = w() - WIDTHMARGIN;
	int realheight = h() - HEIGHTMARGIN;

	fl_clip(x(),y(),w(),h());
	fl_color(FL_BLACK);
	for (int i = 1 ; i < nbSpectrumValues ; i++) {
	    xa = ((i-1)*realwidth)/(nbSpectrumValues-1) + WIDTHMARGIN/2;
	    ya = (int)((1.0 - (spectrum[i-1]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2;
	    xb = (i*realwidth)/(nbSpectrumValues - 1) + WIDTHMARGIN/2;
	    yb = (int)((1.0 - (spectrum[i]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2;
	    dbgprintf("Drawing from: (%d, %d) to (%d, %d)\n",
		      xa, ya, xb, yb);
	    fl_line(xa+x(), ya+y(), xb+x(), yb+y());
	}
	fl_pop_clip();
    }
    
    return; 
}


/// The spectra stuff

spectra::spectra()
{
    dbgprintf("Spectra display dialog created\n");
    spectraWindow = 0; // fluid depends on that
    spectraBox = 0;
    
    return;
}

void spectra::setDefaults(void)
{
    // we display relative values by default
    relative->set(); // button appearance
    spectraBox->setRelative(); // set the variable correctly
    spectrumTitle->value("Position: undefined");
    xvalue->value("0");
    yvalue->value("0");
    // the spectraBox needs to know us
    spectraBox->setSpectra(this);
    
    
    return;
}

// pass on the data...
void spectra::setData(double *s, int nb, int x, int y)
{
    static char buff[100];
    
    spectraBox->setSpectrum(s,nb);

    sprintf(buff, "Position: x=%d, y=%d", x, y);
    xIm = x;
    yIm = y;
    spectrumTitle->value(buff);

    return;
}

int spectra::visible()
{
    return spectraWindow->visible();
}

void spectra::redraw()
{
    spectraBox->redraw();
    return;
}

void spectra::show()
{
    spectraWindow->show();
}

void spectra::hide()
{
    spectraWindow->hide();
}

void spectra::setAxisBoxesLimits(double xminlimit, double xmaxlimit,
				 double yminlimit, double ymaxlimit)
{
    abcissaBox->setRange(xminlimit, xmaxlimit);
    ordinateBox->setRange(yminlimit, ymaxlimit);
    abcissaBox->redraw();
    ordinateBox->redraw();
}


/// The Spectra callbacks.

void relativecheck_cb(Fl_Check_Button *, spectra *panel)
{
    panel->displayRelative();
    return;
}

void absolutecheck_cb(Fl_Check_Button *, spectra *panel)
{
    panel->displayAbsolute();
    return;
}		      
    

void printbutton_cb(Fl_Button *, spectra *)
{
    dbgprintf("Print button pressed\n");
    if (printSpectPanel == 0) {
	printSpectPanel = new printspect;
	printspect &printref = *printSpectPanel;
	// initializes the fluid-generated panel
	printspect_panel(printref);
	// do our own initialization
	printSpectPanel->setDefaults();
    }

    // show the panel
    printSpectPanel->setCallerType(SPECTRAPANEL);
    printSpectPanel->show();
    
    return;
}

void savebutton_cb(Fl_Button *, spectra *)
{
    dbgprintf("Save button pressed\n");
    if (saveSpectPanel == 0) {
	saveSpectPanel = new savespect;
	savespect &saveref = *saveSpectPanel;
	// initializes the fluid-generated panel
	savespect_panel(saveref);
	// do our own initialization
	saveSpectPanel->setDefaults();
    }
    // show the panel
    saveSpectPanel->setCallerType(SPECTRAPANEL);
    saveSpectPanel->show();

    return;
}

void okbutton_cb(Fl_Return_Button*, spectra *panel)
{
    // this is very simple...
    panel->hide();
}