File: bivHist.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 (509 lines) | stat: -rw-r--r-- 12,952 bytes parent folder | download | duplicates (5)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * $Id: bivHist.cxx,v 4.2 2003/06/03 16:11:49 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.
 * */

/*------------------------------------------------------------------------
 *
 * This file contains the code for the bivariate histogram,
 * Both the GUI interface (with bivhist) and the code for the
 * histogram `canvas'
 *
 * Hugues Talbot	26 Apr 1999    
 *  
 *-----------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h> // for sprintf

#include <algorithm>

#include "imview.hxx"
#include "imageIO.hxx"
#include "imageViewer.hxx"
#include "machine.hxx"
#include "bivHist.hxx"
#include "nocase.hxx"
#include "io/newprefio.hxx"


using std::upper_bound;
using std::lower_bound;
using std::sort;


extern imageIO *IOBlackBox;
extern imageViewer *mainViewer;
extern imprefs      *fileprefs;


//
// Comparison function for points
//

// returns true if a is less than b
bool cmpBHPoints::operator() (const BHPoint &a, const BHPoint &b) const
{
    // order by Y first
    if (a.Yvalue < b.Yvalue)
	return true;
    else if (a.Yvalue > b.Yvalue)
	return false;
    // then order by X
    else if (a.Xvalue < b.Xvalue)
	return true;
    else
	return false; // if b <= a
}

//
// Implementation for bivHistBox class.
//

bivHistBox::bivHistBox(int x, int y, int w, int h, const char *l)
    : Fl_Box(x,y,w,h,l)
{
    box(FL_FLAT_BOX);
    color(FL_WHITE);
    dataIsInteger = true;
    xCorrection = yCorrection = 1.0;
    return; 
}


int bivHistBox::handle(int event)
{
    int    retval = 0;
    double xval, yval;

    if (!allPoints.empty()) {

	switch (event) {
	  case FL_ENTER:
	    fl_cursor(FL_CURSOR_CROSS);
	    retval = 1;
	    break;

	  case FL_MOVE: // no button held down
	    retval = handleMouseMoved(xval, yval); // in this case the xval and yval are thrown away
	    break;

	  case FL_PUSH:
	  case FL_DRAG:
	    retval = handleMouseButtonPushed();
	    break;

	  case FL_RELEASE:
	    //IOBlackBox->applyOverlayToCurrentImage();
	    IOBlackBox->applyImageParameters(0, 0, 0, true);
	    mainViewer->displayCurrentImage(); // this can be quite slow 
	    retval = 1;
	    break;
	    
	  case FL_LEAVE:
	    fl_cursor(FL_CURSOR_DEFAULT);
	    retval = 1;
	    break;
	    
	}
    }
    
    return retval;
}

int bivHistBox::handleMouseMoved(double &xval, double &yval)
{
    int                 Xmouse, Ymouse;
    char                p[100];   
    int                 retval = 1;
    double              realw, realh;
    
    Xmouse = Fl::event_x() - x();
    Ymouse = Fl::event_y() - y();

    realw = (double) w() - xCorrection;
    realh = (double) h() - yCorrection;
    
    xval = minXval + ((double)Xmouse * (maxXval - minXval)) / realw;
    yval = minYval + ((h() - (double)Ymouse) * (maxYval - minYval)) / realh;

    if (dataIsInteger) {
	xval = floor(xval);
	yval = floor(yval);
    }

    sprintf(p, "X= %.5g , Y=%.5g", xval, yval);
    
    parentPanel->setPosition(p);   
    
    return retval;
}

int bivHistBox::handleMouseButtonPushed(void)
{
    double              xval, yval, delta, mindelta, distance;
    int                 retval = handleMouseMoved(xval, yval);
    BHPColour           currentColour;
    bhpointIterator     l,u,p;
    BHPoint             highPoint, lowPoint;

    

    if (parentPanel->selectRegionIsOn())
	currentColour = (BHPColour)(parentPanel->getCurrentColour() + 1);
    else
	currentColour = BHPBlack;

    delta = parentPanel->getPenWidth() * (maxYval - minYval)/h() ;
    mindelta = trivmin(delta, parentPanel->getPenWidth() * (maxXval - minXval)/w() );
    
    highPoint = BHPoint(currentColour, 0, xval, yval+delta);
    lowPoint = BHPoint(currentColour, 0, xval, yval-delta);
    // and paint the point...
    u = std::upper_bound(allPoints.begin(), allPoints.end(), highPoint, cmpBHPoints());
    l = std::lower_bound(allPoints.begin(), allPoints.end(), lowPoint, cmpBHPoints());

    dbgprintf("Mouse coordinates: (%g, %g)\n", xval, yval);
    dbgprintf("Lower and upper bounds:\n");
    if (u != allPoints.end()) {
	dbgprintf("    Lower: (%g,%g)\n", (*l).Xvalue, (*l).Yvalue);
    }
    if (l != allPoints.end()) {
	dbgprintf("    Upper: (%g,%g)\n", (*u).Xvalue, (*u).Yvalue);
    }
    if ((l ==  allPoints.end()) && (u == allPoints.end())) {
	dbgprintf(" No upper or lower bounds !\n");
    }
    
    parentPanel->makeCurrent();

    fl_color((int)currentColour);
    
    for (p = l; p != u ; ++p) {
	distance = sqrt(((*p).Xvalue - xval) * ((*p).Xvalue  - xval) + ((*p).Yvalue - yval) * ((*p).Yvalue  - yval));
	if (distance <= mindelta) {
	    (*p).colour = currentColour;
	    drawPointWithJitter((*p).Xvalue, (*p).Yvalue, currentColour);
	    // this is the `linked window' bit.
	    IOBlackBox->setPixelInOverlay((*p).imgOffset, (int)currentColour);
	}
    }

    return retval;
}

void bivHistBox::draw(void)
{
    bhpointIterator  i;

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

    // clip to scroll area dimensions
    parentPanel->getScrollClip(xo,yo,wo,ho);
    fl_clip(xo,yo,wo,ho);

    workoutCorrection();
    
    for (i = allPoints.begin() ; i != allPoints.end() ; ++i) {
	// draw with jitter
	drawPointWithJitter((*i).Xvalue, (*i).Yvalue, (*i).colour);
    }
    
    fl_pop_clip();
    return; 
}

// Work out the size of the jitter. Depends
// a lot on whether the data is integer or double.
void bivHistBox::workoutCorrection(void)
{
    if (dataIsInteger) {
	xCorrection = ((double)w()/(maxXval - minXval));
	yCorrection = ((double)h()/(maxYval - minYval));
    } else {
	xCorrection = 1.0;
	yCorrection = 1.0;
    }
}

// this assumes that the drawing environment is in place: i.e: within draw, or after make_current
// In this function, it turns out that INT_MAX is more portable than RAND_MAX.
void bivHistBox::drawPointWithJitter(double xval, double yval, BHPColour colour)
{
    int              xa, ya;
    double           realwidth,realheight;
    double           jitterX, jitterY;
    
    jitterX = (double)random()/INT_MAX * xCorrection;
    jitterY = (double)random()/INT_MAX * yCorrection;
    
    realwidth = (double)w() - xCorrection;
    realheight =  (double)h() - yCorrection ;
    
    xa = (int)(x() + realwidth*((xval-minXval)/(maxXval-minXval)) + jitterX);
    ya = (int)(y() + realheight*(1.0 - (yval-minYval)/(maxYval-minYval)) + jitterY);

    if ((xa >= xo) && (ya >= yo) && (xa < xo+wo) && (ya < yo+wo)) {
	fl_color(colour);
	fl_point(xa, ya);
    }

    return;
}

void bivHistBox::setData(int Xcomp, int Ycomp)
{
    bhpointIterator i;
    
    IOBlackBox->subsetForBH(allPoints, Xcomp, Ycomp, minXval, minYval, maxXval, maxYval);
    dbgprintf("Collected %d points, minX = %g, minY = %g\n"
	      "                     maxX = %g, maxY = %g\n",
	      allPoints.size(),
	      minXval, minYval,
	      maxXval, maxYval);

    // sample the data and determine if integer or not
    dataIsInteger = true;
    for (i = allPoints.begin(); i < allPoints.end(); i += 101) {
	if ((floor((*i).Xvalue) != (*i).Xvalue)
	    || (floor((*i).Yvalue) != (*i).Yvalue)) {
	    dataIsInteger = false;
	    break;
	}
    }
    if (dataIsInteger)
	dbgprintf("Data is integer\n");
    else
	dbgprintf("Data is floating point\n");
    
    // sort the data
	std::sort(allPoints.begin(), allPoints.end(), cmpBHPoints());
    
    return;
}

// the bivhist implementation
bivhist::bivhist()
{
    dbgprintf("Bivariate histogram dialog created\n");
    bivhistWindow=0; // fluid depend on this behaviour
    histoBox=0;
    return;
}

bivhist::~bivhist()
{
    dbgprintf("Bivariate histogram dialog destroyed\n");

    return;
}

void bivhist::show()
{
    bivhistWindow->show();
}

void bivhist::hide()
{
    bivhistWindow->hide();
}

// The coordinate change in this case is quite difficult
//
void bivhist::enlargeHisto()
{
    int x, y, w, h, x2, y2, w2, h2;
    int relx, rely, newx, newy;

    x = histoBox->x();
    y = histoBox->y();
    w = histoBox->w();
    h = histoBox->h();

    x2 = bivhistScroll->x();
    y2 = bivhistScroll->y();
    w2 = bivhistScroll->w();
    h2 = bivhistScroll->h();

    // relative coordinates
    relx = x2 - x;
    rely = y2 - y;

    newx = -2*relx-w2/2 + x2;
    newy = -2*rely-h2/2 + y2;

    dbgprintf("oldx: %d, relx: %d, newx, %d\n",
	      x, relx, newx);
    
    histoBox->resize(newx, newy, w*2, h*2);

    bivhistScroll->type(Fl_Scroll::BOTH);
    bivhistScroll->redraw();
}

// and this is just as hard
void bivhist::shrinkHisto()
{
    int x, y, w, h, x2, y2, w2, h2;
    int relx, rely, newx, newy;

    x = histoBox->x();
    y = histoBox->y();
    w = histoBox->w();
    h = histoBox->h();

    x2 = bivhistScroll->x();
    y2 = bivhistScroll->y();
    w2 = bivhistScroll->w();
    h2 = bivhistScroll->h();

    // relative coordinates
    relx = x2 - x;
    rely = y2 - y;

    newx = relx/2 + w2/4 + x2;
    newy = rely/2 + h2/4 + y2;
    
    histoBox->resize(newx, newy, w/2, h/2);
    
    bivhistScroll->type(Fl_Scroll::BOTH);
    bivhistScroll->redraw();
}

void bivhist::fitHisto()
{
    int x, y, w, h;

    x = bivhistScroll->x();
    y = bivhistScroll->y();
    w = bivhistScroll->w();
    h = bivhistScroll->h();
    
    histoBox->resize(x+BHP_MARGIN/2, y+BHP_MARGIN/2, w+BHP_MARGIN, h+BHP_MARGIN);

    bivhistScroll->type(0); // get rid of the scrollbars
    bivhistScroll->redraw();
}

// must be called first
void bivhist::setDefaults(void)
{
    // we are viewing comp. #1 vs. #2 by default.
    Xinput->value("0");
    Yinput->value("1");
    histoBox = 0; // very important
    
    return;
}

// the crux of the matter
void bivhist::computeNewBH()
{
    int    Xcomp, Ycomp;
    double minX, minY, maxX, maxY;
    // get all the elements
    Xcomp = atoi(Xinput->value());
    Ycomp = atoi(Yinput->value());
    // delete the previous canvas
    if (histoBox) {
	bivhistScroll->remove(histoBox);
	delete histoBox; // this should destroy all the data associated with histoBox
    }

    // create a new bivHistBox with the same dimensions as the current canvas
    histoBox = new bivHistBox(bivhistScroll->x()+BHP_MARGIN/2, bivhistScroll->y()+BHP_MARGIN/2,
			      bivhistScroll->w()-BHP_MARGIN, bivhistScroll->h()-BHP_MARGIN);

    histoBox->setParentPanel(this);
    bivhistScroll->add(histoBox);

    histoBox->setData(Xcomp, Ycomp);

    // set the limits
    histoBox->limits(minX, minY, maxX, maxY);
    absissaBox->setRange(minX, maxX);
    ordinateBox->setRange(minY, maxY);

    // redraw everything
    bivhistWindow->redraw();
    
    return;
}

void bivhist::setTransparency(double v)
{
    transparencyValue->value(v);
    fileprefs->overlayTransparency(v);
    mainViewer->displayCurrentImage(); // this should take the new transparency into account
}

////////// All the bivhist callbacks

void computebivhist_cb(Fl_Return_Button*, bivhist *panel)
{
    dbgprintf("Computing a new 2-D histogram\n");
    panel->computeNewBH();
}

void dismissbutton_cb(Fl_Button*, bivhist *panel)
{
    panel->hide();
}


void setpenwidth_cb(Fl_Roller *r, bivhist *panel)
{
    panel->setPenWidth(r->value());
}

void zoominbutton_cb(Fl_Button*, bivhist *panel)
{
    panel->enlargeHisto();
}

void zoomoutbutton_cb(Fl_Button*, bivhist *panel)
{
    panel->shrinkHisto();
}

void resetbutton_cb(Fl_Button*, bivhist *panel)
{
    panel->fitHisto();
}

void settransparency_cb(Fl_Roller *r, bivhist *panel)
{
    panel->setTransparency(r->value());
}