-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaint_utils.c
More file actions
410 lines (360 loc) · 10.3 KB
/
paint_utils.c
File metadata and controls
410 lines (360 loc) · 10.3 KB
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
#if defined(__GNUC__)
#ident "University of Edinburgh $Id$"
#else
static char _paint_utils_c[] = "University of Edinburgh $Id$";
#endif
/*!
* \file paint_utils.c
* \author Richard Baldock
* \date April 2009
* \version $Id$
* \par
* Address:
* MRC Human Genetics Unit,
* MRC Institute of Genetics and Molecular Medicine,
* University of Edinburgh,
* Western General Hospital,
* Edinburgh, EH4 2XU, UK.
* \par
* Copyright (C), [2012],
* The University Court of the University of Edinburgh,
* Old College, Edinburgh, UK.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
* \ingroup MAPaint
* \brief
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <MAPaint.h>
#include <X11/Xproto.h>
void myHGU_XmHelpStandardCb(Widget w,
XtPointer client_data,
XtPointer call_data)
{
/* dummy routine to switch off help */
HGU_XmUserMessage(w,
"Button and context help has been\n"
"disabled to switch to a more usable\n"
"system based on JavaHelp.\n\n"
"For now please see help pages on the\n"
"Mouse Atlas web site:\n"
"http://genex.hgu.mrc.ac.uk/Resources/"
"or email ma-tech@hgu.mrc.ac.uk.\n"
"Thank you",
XmDIALOG_FULL_APPLICATION_MODAL);
return;
}
void popup_dialog_cb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
XtManageChild( (Widget) client_data );
XtPopup( XtParent((Widget) client_data), XtGrabNone );
/* kludge to update the file selections */
if( XmIsFileSelectionBox((Widget) client_data) ){
XtCallCallbacks((Widget) client_data, XmNmapCallback, call_data);
}
return;
}
void UnmanageChildCb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
if( client_data != NULL ){
XtUnmanageChild( (Widget) client_data );
}
else {
XtUnmanageChild( w );
}
return;
}
void destroy_cb(
Widget w,
XtPointer client_data,
XtPointer call_data)
{
XtDestroyWidget( (Widget) client_data );
return;
}
int InteractDispatchEvent(
Display *dpy,
Window win,
XtPointer client_data,
XEvent *event)
{
XtDispatchEvent( event );
return( globals.paint_action_quit_flag );
}
int InteractHelpCb(
Display *dpy,
Window win,
XtPointer client_data,
String help_str)
{
HGU_XmHelpShowString( globals.topl, (String) client_data, help_str );
return( 0 );
}
int set_grey_values_from_ref_object(
WlzObject *obj,
WlzObject *ref_obj)
{
WlzObject *objs[3], *new_obj;
WlzPlaneDomain *pdom, *pdom1, *pdom2;
WlzVoxelValues *voxvals1, *voxvals2;
WlzIntervalWSpace iwsp1, iwsp2;
WlzGreyWSpace gwsp1, gwsp2;
int p;
WlzPixelV min, max, Min, Max;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* check the object */
if((obj == NULL) || (obj->type != ref_obj->type) ||
((obj->type != WLZ_2D_DOMAINOBJ) && (obj->type != WLZ_3D_DOMAINOBJ))){
return( -1 );
}
/* set the required grey ranges */
min.type = WLZ_GREY_UBYTE;
max.type = WLZ_GREY_UBYTE;
Min.type = WLZ_GREY_UBYTE;
Max.type = WLZ_GREY_UBYTE;
min.v.ubv = 0;
max.v.ubv = 255;
Min.v.ubv = globals.cmapstruct->gmin;
Max.v.ubv = globals.cmapstruct->gmax;
/* scan through the new domain */
objs[0] = WlzAssignObject(obj, NULL);
objs[1] = WlzAssignObject(globals.obj, NULL);
objs[2] = WlzAssignObject(ref_obj, NULL);
new_obj = WlzIntersectN(3, objs, 0, &errNum);
WlzFreeObj(objs[0]);
WlzFreeObj(objs[1]);
WlzFreeObj(objs[2]);
if( errNum == WLZ_ERR_NONE ){
switch( obj->type ){
default:
errNum = WLZ_ERR_OBJECT_TYPE;
break;
case WLZ_2D_DOMAINOBJ:
if((objs[0] = WlzMakeMain(WLZ_2D_DOMAINOBJ,
new_obj->domain,
globals.obj->values,
NULL, NULL, &errNum))){
if((objs[1] = WlzMakeMain(WLZ_2D_DOMAINOBJ,
new_obj->domain,
ref_obj->values,
NULL, NULL, &errNum))){
errNum = WlzInitGreyScan( objs[0], &iwsp1, &gwsp1 );
errNum |= WlzInitGreyScan( objs[1], &iwsp2, &gwsp2 );
while((errNum == WLZ_ERR_NONE) &&
((errNum = WlzNextGreyInterval(&iwsp1)) == WLZ_ERR_NONE) &&
(WlzNextGreyInterval(&iwsp2) == WLZ_ERR_NONE) )
{
memcpy((void *) gwsp1.u_grintptr.ubp,
(const void *) gwsp2.u_grintptr.ubp,
(size_t) (iwsp1.rgtpos - iwsp1.lftpos + 1));
}
if( errNum == WLZ_ERR_EOO ){
errNum = WLZ_ERR_NONE;
}
WlzGreySetRange(objs[0], min, max, Min, Max, 0);
WlzFreeObj( objs[1] );
}
WlzFreeObj( objs[0] );
}
break;
case WLZ_3D_DOMAINOBJ:
pdom = new_obj->domain.p;
pdom1 = globals.obj->domain.p;
pdom2 = ref_obj->domain.p;
voxvals1 = globals.obj->values.vox;
voxvals2 = ref_obj->values.vox;
for(p=pdom->plane1; p <= pdom->lastpl; p++){
if( pdom->domains[p - pdom->plane1].core == NULL ){
continue;
}
if((objs[0] = WlzMakeMain(WLZ_2D_DOMAINOBJ,
pdom->domains[p - pdom->plane1],
voxvals1->values[p - pdom1->plane1],
NULL, NULL, &errNum))){
if((objs[1] = WlzMakeMain(WLZ_2D_DOMAINOBJ,
pdom->domains[p - pdom->plane1],
voxvals2->values[p - pdom2->plane1],
NULL, NULL, &errNum))){
errNum = WlzInitGreyScan( objs[0], &iwsp1, &gwsp1 );
errNum |= WlzInitGreyScan( objs[1], &iwsp2, &gwsp2 );
while((errNum == WLZ_ERR_NONE) &&
((errNum = WlzNextGreyInterval(&iwsp1)) == WLZ_ERR_NONE) &&
(WlzNextGreyInterval(&iwsp2) == WLZ_ERR_NONE) )
{
memcpy((void *) gwsp1.u_grintptr.ubp,
(const void *) gwsp2.u_grintptr.ubp,
(size_t) (iwsp1.rgtpos - iwsp1.lftpos + 1));
}
if( errNum == WLZ_ERR_EOO ){
errNum = WLZ_ERR_NONE;
}
WlzGreySetRange(objs[0], min, max, Min, Max, 0);
WlzFreeObj( objs[1] );
}
WlzFreeObj( objs[0] );
}
}
break;
}
}
if( new_obj ){
WlzFreeObj( new_obj );
}
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "set_grey_values_from_ref_object", errNum);
}
return( 0 );
}
static int set_grey_values_from_domain3D(
WlzObject *obj,
WlzObject *destObj,
unsigned int col,
unsigned int planes)
{
WlzObject *obj1, *new_obj;
WlzPlaneDomain *pdom, *pdom1;
WlzVoxelValues *voxvaltb;
WlzIntervalWSpace iwsp;
WlzGreyWSpace gwsp;
int k, p;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* check the object */
if( obj == NULL || obj->type != WLZ_3D_DOMAINOBJ )
return( -1 );
/* scan through the new domain */
if(((new_obj = WlzIntersect2(obj, destObj, &errNum)) == NULL) ||
(new_obj->type == WLZ_EMPTY_OBJ) ){
HGU_XmUserMessage(globals.topl,
"The domain object just read has no\n"
"intersection with the reference object",
XmDIALOG_FULL_APPLICATION_MODAL);
if( new_obj ){
WlzFreeObj( new_obj );
}
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "set_grey_values_from_domain3D",
errNum);
}
return( 0 );
}
pdom = new_obj->domain.p;
pdom1 = destObj->domain.p;
voxvaltb = destObj->values.vox;
for(p=pdom->plane1; p <= pdom->lastpl; p++){
if( pdom->domains[p - pdom->plane1].core == NULL )
continue;
if((obj1 = WlzMakeMain(WLZ_2D_DOMAINOBJ,
pdom->domains[p - pdom->plane1],
voxvaltb->values[p - pdom1->plane1],
NULL, NULL, &errNum))){
errNum = WlzInitGreyScan( obj1, &iwsp, &gwsp );
while((errNum == WLZ_ERR_NONE) &&
((errNum = WlzNextGreyInterval(&iwsp)) == WLZ_ERR_NONE) ){
for( k=0; k <= (iwsp.rgtpos - iwsp.lftpos); k++ ){
gwsp.u_grintptr.ubp[k] &= ~planes;
gwsp.u_grintptr.ubp[k] |= col;
}
}
if( errNum == WLZ_ERR_EOO ){
errNum = WLZ_ERR_NONE;
}
WlzFreeObj( obj1 );
}
}
WlzFreeObj( new_obj );
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "set_grey_values_from_domain3D", errNum);
}
return( 0 );
}
int set_grey_values_from_domain(
WlzObject *obj,
WlzObject *destObj,
unsigned int col,
unsigned int planes)
{
WlzObject *new_obj;
WlzIntervalWSpace iwsp;
WlzGreyWSpace gwsp;
int k;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* check the object */
if( obj == NULL ){
return( -1 );
}
/* check the object type */
if( obj->type != destObj->type ){
return( -1 );
}
if( obj->type == WLZ_3D_DOMAINOBJ ){
return set_grey_values_from_domain3D(obj, destObj, col, planes);
}
/* scan through the new domain */
if((new_obj = WlzIntersect2(obj, destObj, &errNum))){
new_obj = WlzAssignObject(new_obj, NULL);
if( WlzArea(new_obj, &errNum) > 0 ){
new_obj->values = WlzAssignValues(destObj->values, NULL);
errNum = WlzInitGreyScan( new_obj, &iwsp, &gwsp );
while((errNum == WLZ_ERR_NONE) &&
((errNum = WlzNextGreyInterval(&iwsp)) == WLZ_ERR_NONE) ){
for( k=0; k <= (iwsp.rgtpos - iwsp.lftpos); k++ ){
gwsp.u_grintptr.ubp[k] &= ~planes;
gwsp.u_grintptr.ubp[k] |= col;
}
}
if( errNum == WLZ_ERR_EOO ){
errNum = WLZ_ERR_NONE;
}
}
WlzFreeObj(new_obj);
}
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "set_grey_values_from_domain", errNum);
return 1;
}
return 0;
}
void MAPaintReportWlzError(
Widget w,
String srcStr,
WlzErrorNum wlzErr)
{
char msgBuf[512];
const char *errStr, *msgStr;
/* get the error and message strings */
errStr = WlzStringFromErrorNum(wlzErr, &msgStr);
/* create the message string to display */
sprintf(msgBuf,
"A Woolz error has occurred in %s:\n\n"
"\t%s: %s\n\n"
"Safest action is to try and save domains\n"
"and restart paint. If that fails you can\n"
"recover data from the autosave file.\n\n"
"Please report this error to:\n"
"ma-tech@hgu.mrc.ac.uk",
srcStr, errStr, msgStr);
HGU_XmUserError(w, msgBuf, XmDIALOG_FULL_APPLICATION_MODAL);
return;
}