/
statusview.c
487 lines (391 loc) · 11.2 KB
/
statusview.c
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
/* widgets for the status bar
*/
/*
Copyright (C) 1991-2003 The National Gallery
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-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#include "ip.h"
static GtkFrameClass *parent_class = NULL;
/* The popup menu.
*/
static GtkWidget *statusview_menu = NULL;
/* Sub. fn. of below. Junk the widgets inside a band display.
*/
static void *
statusviewband_destroy_sub( StatusviewBand *svb )
{
DESTROY_GTK( svb->val );
IM_FREE( svb );
return( NULL );
}
/* Junk the widgets inside a band display.
*/
static void
statusviewband_destroy( Statusview *sv )
{
slist_map( sv->bands,
(SListMapFn) statusviewband_destroy_sub, NULL );
IM_FREEF( g_slist_free, sv->bands );
}
static void
statusview_destroy( GtkObject *object )
{
Statusview *sv;
g_return_if_fail( object != NULL );
g_return_if_fail( IS_STATUSVIEW( object ) );
sv = STATUSVIEW( object );
#ifdef DEBUG
printf( "statusview_destroy\n" );
#endif /*DEBUG*/
statusviewband_destroy( sv );
GTK_OBJECT_CLASS( parent_class )->destroy( object );
}
/* Hide this statusview.
*/
static void
statusview_hide_cb( GtkWidget *menu, GtkWidget *host, Statusview *sv )
{
sv->imagemodel->show_status = FALSE;
iobject_changed( IOBJECT( sv->imagemodel ) );
}
static void
statusview_class_init( StatusviewClass *class )
{
GtkObjectClass *object_class = (GtkObjectClass *) class;
GtkWidget *pane;
parent_class = g_type_class_peek_parent( class );
object_class->destroy = statusview_destroy;
/* Create signals.
*/
/* Init methods.
*/
pane = statusview_menu = popup_build( _( "Status bar menu" ) );
popup_add_but( pane, GTK_STOCK_CLOSE,
POPUP_FUNC( statusview_hide_cb ) );
}
static void
statusview_init( Statusview *sv )
{
GtkWidget *vb, *hb;
GtkWidget *eb;
sv->imagemodel = NULL;
sv->bands = NULL;
sv->fmt = -1;
sv->nb = -1;
gtk_frame_set_shadow_type( GTK_FRAME( sv ), GTK_SHADOW_OUT );
eb = gtk_event_box_new();
gtk_container_add( GTK_CONTAINER( sv ), eb );
popup_attach( eb, statusview_menu, sv );
vb = gtk_vbox_new( FALSE, 0 );
gtk_container_set_border_width( GTK_CONTAINER( vb ), 1 );
gtk_container_add( GTK_CONTAINER( eb ), vb );
sv->top = gtk_label_new( "" );
gtk_misc_set_alignment( GTK_MISC( sv->top ), 0.0, 0.5 );
gtk_box_pack_start( GTK_BOX( vb ), sv->top, TRUE, TRUE, 0 );
hb = gtk_hbox_new( FALSE, 5 );
gtk_box_pack_start( GTK_BOX( vb ), hb, TRUE, TRUE, 0 );
sv->pos = gtk_label_new( "" );
set_fixed( sv->pos, strlen( "(888888,888888)" ) );
gtk_misc_set_alignment( GTK_MISC( sv->pos ), 0.0, 0.5 );
gtk_box_pack_start( GTK_BOX( hb ), sv->pos, FALSE, FALSE, 0 );
sv->hb = gtk_hbox_new( FALSE, 5 );
gtk_box_pack_start( GTK_BOX( hb ), sv->hb, TRUE, TRUE, 0 );
sv->mag = gtk_label_new( "" );
gtk_misc_set_alignment( GTK_MISC( sv->mag ), 0.0, 0.5 );
gtk_box_pack_end( GTK_BOX( hb ), sv->mag, FALSE, FALSE, 0 );
gtk_widget_show_all( eb );
}
GtkType
statusview_get_type( void )
{
static GtkType statusview_type = 0;
if( !statusview_type ) {
static const GtkTypeInfo sinfo = {
"Statusview",
sizeof( Statusview ),
sizeof( StatusviewClass ),
(GtkClassInitFunc) statusview_class_init,
(GtkObjectInitFunc) statusview_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL,
};
statusview_type =
gtk_type_unique( GTK_TYPE_FRAME, &sinfo );
}
return( statusview_type );
}
/* Our model has changed ... update.
*/
static void
statusview_changed_cb( Imagemodel *imagemodel, Statusview *sv )
{
static char *sample[] = {
/* Sample text for each BandFmt. Used to try to get
* the spacing right.
*/
"888", /* uchar */
"-888", /* char */
"88888", /* ushort */
"-88888", /* short */
"888888888", /* int */
"-888888888", /* uint */
"888888888", /* float */
"(88888888,888888888)", /* complex */
"88888888888", /* double */
"(8888888888,888888888)" /* dpcomplex */
};
Conversion *conv = imagemodel->conv;
iImage *iimage = imagemodel->iimage;
IMAGE *im = imageinfo_get( FALSE, iimage->value.ii );
double size = (double) im->Ysize * IM_IMAGE_SIZEOF_LINE( im );
unsigned int nb;
int fmt;
char txt[MAX_LINELENGTH];
VipsBuf buf = VIPS_BUF_STATIC( txt );
#ifdef DEBUG
printf( "statusview_changed_cb: %p\n", imagemodel );
#endif /*DEBUG*/
widget_visible( GTK_WIDGET( sv ), imagemodel->show_status );
/* If we're hidden, no need to do any more.
*/
if( !imagemodel->show_status )
return;
if( conv->mag > 0 )
set_glabel( sv->mag, "%s %d:1",
_( "Magnification" ), conv->mag );
else
set_glabel( sv->mag, "%s 1:%d",
_( "Magnification" ), -conv->mag );
vips_buf_appendf( &buf, "%s, ",
NN( IOBJECT( iimage )->caption ) );
vips_buf_append_size( &buf, size );
vips_buf_appendf( &buf, ", %.3gx%.3g p/mm", im->Xres, im->Yres );
set_gcaption( sv->top, "%s", vips_buf_all( &buf ) );
if( im->Coding == IM_CODING_LABQ ||
im->Coding == IM_CODING_RAD ) {
nb = 3;
fmt = 6;
}
else {
nb = im->Bands;
fmt = im->BandFmt;
}
if( sv->nb != nb || sv->fmt != fmt ) {
/* Bands/fmt has changed ... rebuild band display widgets.
*/
unsigned int i;
int width;
statusviewband_destroy( sv );
sv->fmt = fmt;
sv->nb = nb;
if( sv->fmt >= 0 && sv->fmt < IM_NUMBER( sample ) )
width = strlen( sample[sv->fmt] );
else
width = 10;
/* Don't display more than 8 bands ... it'll make the window
* too large.
FIXME ... now very kewl
*/
for( i = 0; i < IM_MIN( 8, nb ); i++ ) {
StatusviewBand *band = INEW( NULL, StatusviewBand );
band->sv = sv;
band->bandno = i;
band->val = gtk_label_new( "" );
gtk_misc_set_alignment( GTK_MISC( band->val ),
0.0, 0.5 );
set_fixed( band->val, width );
gtk_box_pack_start( GTK_BOX( sv->hb ),
band->val, FALSE, FALSE, 0 );
gtk_widget_show( band->val );
sv->bands = g_slist_append( sv->bands, band );
}
}
}
static void
statusview_link( Statusview *sv, Imagemodel *imagemodel )
{
sv->imagemodel = imagemodel;
g_signal_connect( G_OBJECT( sv->imagemodel ),
"changed", G_CALLBACK( statusview_changed_cb ), sv );
}
Statusview *
statusview_new( Imagemodel *imagemodel )
{
Statusview *sv = gtk_type_new( TYPE_STATUSVIEW );
statusview_link( sv, imagemodel );
return( sv );
}
/* Turn a IM_CODING_LABQ 4-band image into three floats.
*/
static void
statusview_mouse_LABPACK( Statusview *sv, int x, int y )
{
Imagemodel *imagemodel = sv->imagemodel;
Conversion *conv = imagemodel->conv;
GSList *bands = sv->bands;
/* Three widgets we update.
*/
StatusviewBand *b1;
StatusviewBand *b2;
StatusviewBand *b3;
unsigned char *e =
(unsigned char *) get_element( conv->reg, x, y, 0 );
unsigned int iL = (e[0] << 2) | (e[3] >> 6);
float L = 100.0 * iL / 1023.0;
signed int ia = ((signed char) e[1] << 3) | ((e[3] >> 3) & 0x7);
float a = 0.125 * ia;
signed int ib = ((signed char) e[2] << 3) | (e[3] & 0x7);
float b = 0.125 * ib;
if( g_slist_length( sv->bands ) == 3 ) {
b1 = (StatusviewBand *) bands->data;
b2 = (StatusviewBand *) bands->next->data;
b3 = (StatusviewBand *) bands->next->next->data;
set_glabel( b1->val, "%g", L );
set_glabel( b2->val, "%g", a );
set_glabel( b3->val, "%g", b );
}
}
/* Turn a IM_CODING_RAD 4-band image into three floats.
*/
static void
statusview_mouse_RAD( Statusview *sv, int x, int y )
{
Imagemodel *imagemodel = sv->imagemodel;
Conversion *conv = imagemodel->conv;
GSList *bands = sv->bands;
/* Three widgets we update.
*/
StatusviewBand *b1;
StatusviewBand *b2;
StatusviewBand *b3;
unsigned char *e =
(unsigned char *) get_element( conv->reg, x, y, 0 );
double f = ldexp( 1.0, e[3] - (128 + 8) );
float r = (e[0] + 0.5) * f;
float g = (e[1] + 0.5) * f;
float b = (e[2] + 0.5) * f;
if( g_slist_length( sv->bands ) == 3 ) {
b1 = (StatusviewBand *) bands->data;
b2 = (StatusviewBand *) bands->next->data;
b3 = (StatusviewBand *) bands->next->next->data;
set_glabel( b1->val, "%g", r );
set_glabel( b2->val, "%g", g );
set_glabel( b3->val, "%g", b );
}
}
/* Sub-fn of below. Remake a band in the bar.
*/
static void *
statusview_mouse_band( StatusviewBand *svb, void *e )
{
Imagemodel *imagemodel = svb->sv->imagemodel;
Conversion *conv = imagemodel->conv;
REGION *reg = conv->reg;
IMAGE *im = reg->im;
/* Generate string for contents of band element.
*/
if( im->Coding == IM_CODING_NONE )
switch( im->BandFmt ) {
case IM_BANDFMT_UCHAR:
set_glabel( svb->val, "%d",
((unsigned char *)e)[svb->bandno] );
break;
case IM_BANDFMT_CHAR:
set_glabel( svb->val, "%d",
((char *)e)[svb->bandno] );
break;
case IM_BANDFMT_USHORT:
set_glabel( svb->val, "%d",
((unsigned short *)e)[svb->bandno] );
break;
case IM_BANDFMT_SHORT:
set_glabel( svb->val, "%d",
((short *)e)[svb->bandno] );
break;
case IM_BANDFMT_UINT:
set_glabel( svb->val, "%d",
((unsigned int *)e)[svb->bandno] );
break;
case IM_BANDFMT_INT:
set_glabel( svb->val, "%d",
((int *)e)[svb->bandno] );
break;
case IM_BANDFMT_FLOAT:
set_glabel( svb->val, "%g",
((float *)e)[svb->bandno] );
break;
case IM_BANDFMT_COMPLEX:
set_glabel( svb->val, "(%g,%g)",
((float *)e)[svb->bandno << 1],
((float *)e)[(svb->bandno << 1) + 1] );
break;
case IM_BANDFMT_DOUBLE:
set_glabel( svb->val, "%g",
((double *)e)[svb->bandno] );
break;
case IM_BANDFMT_DPCOMPLEX:
set_glabel( svb->val, "(%g,%g)",
((double *)e)[svb->bandno << 1],
((double *)e)[(svb->bandno << 1) + 1] );
break;
default:
set_glabel( svb->val, "???" );
break;
}
else
set_glabel( svb->val, "???" );
return( NULL );
}
void
statusview_mouse( Statusview *sv, int x, int y )
{
Imagemodel *imagemodel = sv->imagemodel;
Conversion *conv = imagemodel->conv;
IMAGE *im = imageinfo_get( FALSE, conv->ii );
REGION *reg = conv->reg;
double dx, dy;
x = IM_CLIP( 0, x, conv->underlay.width - 1 );
y = IM_CLIP( 0, y, conv->underlay.height - 1 );
/* Calculate x/y pos we display.
*/
dx = x;
dy = y;
if( imagemodel->rulers_offset ) {
dx -= im->Xoffset;
dy -= im->Yoffset;
}
if( imagemodel->rulers_mm ) {
dx /= im->Xres;
dy /= im->Yres;
}
set_glabel( sv->pos, "(%5g, %5g)", dx, dy );
/* Update value list.
*/
if( reg ) {
if( reg->im->Coding == IM_CODING_LABQ )
statusview_mouse_LABPACK( sv, x, y );
else if( reg->im->Coding == IM_CODING_RAD )
statusview_mouse_RAD( sv, x, y );
else
slist_map( sv->bands,
(SListMapFn) statusview_mouse_band,
get_element( reg, x, y, 0 ) );
}
}