File: profileBox.cxx

package info (click to toggle)
imview 1.1.9c-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,068 kB
  • ctags: 3,686
  • sloc: cpp: 28,834; sh: 2,624; ansic: 1,818; makefile: 767; exp: 112; python: 88
file content (455 lines) | stat: -rw-r--r-- 11,165 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * $Id: profileBox.cxx,v 4.0 2003/04/28 14:39:57 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 profile display panel
 *
 * Hugues Talbot	 1 Aug 1998
 *      
 *-----------------------------------------------------------------------*/

#include <string.h>
#include "imview.hxx"
#include "imageIO.hxx"
#include "imageViewer.hxx"
#include "profileBox.hxx"
#include "saveSpect.hxx"
#include "printSpect.hxx"

extern imageViewer      *mainViewer;
extern savespect        *saveSpectPanel;
extern printspect       *printSpectPanel;

void myProfileBox::setProfiles(int startX,
			       int startY,
			       int endX,
			       int endY,
                               double *theProfileR,
			       double *theProfileG,
			       double *theProfileB,
			       int nbval)
{
    if (theProfileR != 0) {
	if (profileR) delete[] profileR;
	if (profileG) delete[] profileG;
	if (profileB) delete[] profileB;
	// R will be either Red or Grey
	profileR = new double[nbval];
	memcpy(profileR, theProfileR, nbval*sizeof(double));

	if (theProfileG) {
	    profileG = new double[nbval];
	    memcpy(profileG, theProfileG, nbval*sizeof(double));
	} else {
	    profileG = 0;
	}
	if (theProfileB) {
	    profileB = new double[nbval];
	    memcpy(profileB, theProfileB, nbval*sizeof(double));
	} else {
	    profileB = 0;
	}
	nbProfileValues = nbval;
	sx = startX ; sy = startY;
	ex = endX; ey = endY;
	recomputeLimits();
    } else {
	if (profileR) delete[] profileR;
	if (profileG) delete[] profileG;
	if (profileB) delete[] profileB;
	
	profileR = profileG = profileB = 0;
	nbProfileValues = 0;
    }

    return;
}

void myProfileBox::recomputeLimits(void)
{
    if (profileR) {
	if (!absolute)
	    mins = maxs = profileR[0];
	// else they have already been set to 0
	for (int i = 1 ; i < nbProfileValues ; i++) {
	    if (profileR[i] > maxs) maxs = profileR[i];
	    if (profileR[i] < mins) mins = profileR[i];
	}
	// same for G and B if they exist
	if (profileG) {
	    for (int i = 1 ; i < nbProfileValues ; i++) {
		if (profileG[i] > maxs) maxs = profileG[i];
		if (profileG[i] < mins) mins = profileG[i];
	    }
	}
	if (profileB) {
	    for (int i = 1 ; i < nbProfileValues ; i++) {
		if (profileB[i] > maxs) maxs = profileB[i];
		if (profileB[i] < mins) mins = profileB[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 (theProfile) {
	    theProfile->setAxisBoxesLimits(0, nbProfileValues,
					   mins, maxs);
	}
    }

    return;
}

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

    if (profileR) {
	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 myProfileBox::handleButtonPushed()
{

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

void myProfileBox::handleButtonDragged()
{
    int index;
    int xx = Fl::event_x();
    int realwidth = w() - WIDTHMARGIN;
    
    index = (int)(((double)xx-x()-WIDTHMARGIN/2)*(nbProfileValues-1)/realwidth + 0.5);
    if ((index >= 0) && (index < nbProfileValues) && theProfile) {
	theProfile->setXValue((int)(sx + (double)index*(ex-sx)/nbProfileValues));
	theProfile->setYValue((int)(sy + (double)index*(ey-sy)/nbProfileValues));
	theProfile->setRValue(profileR[index]);
	if (profileG)
	    theProfile->setGValue(profileG[index]);
	else
	    theProfile->setGValue(profileR[index]);
	if (profileB)
	    theProfile->setBValue(profileB[index]);
	else
	    theProfile->setBValue(profileR[index]);
    } 
    return;
}

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

    return;
}

void myProfileBox::draw()
{
    int xa, ya, xb, yb;
    int realwidth = w() - WIDTHMARGIN;
    int realheight = h() - HEIGHTMARGIN;

    // superclass draw:
    draw_box();
    draw_label();

    fl_clip(x(),y(),w(),h());

    if (profileR != 0) {
	if (profileG != 0)
	    fl_color(FL_RED);
	else
	    fl_color(FL_BLACK);
	for (int i = 1 ; i < nbProfileValues ; i++) {
	    xa = (int)(((i-1)*realwidth)/(nbProfileValues-1) + WIDTHMARGIN/2 + 0.5);
	    ya = (int)(((1.0 - (profileR[i-1]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 + 0.5);
	    xb = (int)((i*realwidth)/(nbProfileValues - 1) + WIDTHMARGIN/2 + 0.5);
	    yb = (int)(((1.0 - (profileR[i]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 +0.5);
	    fl_line(xa+x(), ya+y(), xb+x(), yb+y());
	}
    }
    
    if (profileG != 0) {
	fl_color(FL_GREEN);
	for (int i = 1 ; i < nbProfileValues ; i++) {
	    xa = (int)(((i-1)*realwidth)/(nbProfileValues-1) + WIDTHMARGIN/2 + 0.5);
	    ya = (int)(((1.0 - (profileG[i-1]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 +0.5);
	    xb = (int)((i*realwidth)/(nbProfileValues - 1) + WIDTHMARGIN/2 + 0.5);
	    yb = (int)(((1.0 - (profileG[i]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 + 0.5);
	    fl_line(xa+x(), ya+y(), xb+x(), yb+y());
	}
    }

    if (profileB != 0) {
	fl_color(FL_BLUE);
	for (int i = 1 ; i < nbProfileValues ; i++) {
	    xa = (int)(((i-1)*realwidth)/(nbProfileValues-1) + WIDTHMARGIN/2 + 0.5);
	    ya = (int)(((1.0 - (profileB[i-1]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 + 0.5);
	    xb = (int)((i*realwidth)/(nbProfileValues - 1) + WIDTHMARGIN/2 + 0.5);
	    yb = (int)(((1.0 - (profileB[i]-mins)/(maxs-mins))*realheight) + HEIGHTMARGIN/2 + 0.5);
	    fl_line(xa+x(), ya+y(), xb+x(), yb+y());
	}
    }
    
    fl_pop_clip();
    return; 
}


/// The profile stuff

profile::profile()
{
    dbgprintf("Profile display dialog created\n");
    profileWindow = 0; // fluid depends on that
    profileBox = 0;
    
    return;
}

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

// pass on the data...
void profile::setData(double *R,
		      double *G,
		      double *B,
		      int nb,
		      int startx, int starty,
		      int endx, int endy)
{
    static char buff[100];
    
    profileBox->setProfiles(startx, starty,
			    endx, endy,
			    R, G, B,
			    nb);

    sprintf(buff, "Position: xs=%d, ys=%d, xe=%d, ye=%d",
	    startx, starty,
	    endx, endy);
    xS = startx;
    yS = starty;
    xE = endx;
    yE = endy;
    profileTitle->value(buff);

    return;
}

int profile::visible()
{
    return profileWindow->visible();
}

void profile::redraw()
{
    profileBox->redraw();
    return;
}

void profile::show()
{
    profileWindow->show();
}

void profile::hide()
{
    profileWindow->hide();
}

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

void profile::setXValue(int xv) {
    static char a[10];
    sprintf(a, "%d", xv);
    xvalue->value(a);
}

void profile::setYValue(int yv) {
    static char a[30];
    sprintf(a, "%d", yv);
    yvalue->value(a);
}

void profile::setZValue(int zv) {
    static char a[30];
    sprintf(a, "%d", zv);
    zvalue->value(a);
}

void profile::setRValue(double Rv) {
    static char a[10];
    sprintf(a, "%g", Rv);
    Rvalue->value(a);
}

void profile::setGValue(double Gv) {
    static char a[30];
    sprintf(a, "%g", Gv);
    Gvalue->value(a);
}

void profile::setBValue(double Bv) {
    static char a[30];
    sprintf(a, "%g", Bv);
    Bvalue->value(a);
}

/// The Profile callbacks.

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

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

void keepline_cb(Fl_Check_Button *b, profile *)
{
    if (b->value()) 
	mainViewer->keepLine();
    else
	mainViewer->removeLine();
    return;
}

void printbutton_cb(Fl_Button *, profile *)
{
    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(PROFILEPANEL);
    printSpectPanel->show();
    
    return;
}

void savebutton_cb(Fl_Button *, profile *)
{
    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(PROFILEPANEL);
    saveSpectPanel->show();

    return;
}

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