-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_X11.sc
539 lines (433 loc) · 12.6 KB
/
print_X11.sc
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* tab:8
*
* print_X11.sc - output images to X11
*
*
* "Copyright (c) 1994,1995 The Regents of the University of Maryland.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF MARYLAND HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: David A. Bader <dbader@umiacs.umd.edu>
* Joseph F. Ja'Ja' <joseph@umiacs.umd.edu>
* Institute for Advanced Computer Studies
* Department of Electrical Engineering
* AV Williams Building
* College Park, MD 20742
*
* Version: 1
* Creation Date: November 9, 1995
* Filename: print_X11.sc
* History:
*/
#if X11
#include "print_X11.h"
#include "connComp_gen.h"
#include "radixsort.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <math.h>
#define _WINDOWNAME "Image Window"
#define _ICONNAME "ImageIcon"
#define MAXWINDOWS 10
#define _NCOLORSMAX 64
static Display *_display;
static GC _gc;
static int _screen;
static unsigned long _fg, _bg;
static XSizeHints _hint;
static Visual *_visual;
static unsigned int _depth;
static int _borderwidth = 7;
static int _initX = 0;
static char *_windowname = _WINDOWNAME;
static char *_iconname = _ICONNAME;
static int _NCOLORS = _NCOLORSMAX;
static Colormap _cmap;
static XColor _colors[_NCOLORSMAX];
static
void init_X(int width, int height)
{
char _name[80];
XColor _exact_colors[_NCOLORSMAX];
Status _stat;
int i;
_display = XOpenDisplay("");
_screen = DefaultScreen(_display);
_bg = WhitePixel(_display, _screen);
_fg = BlackPixel(_display, _screen);
_hint.x = 100;
_hint.y = 150;
_hint.width = width;
_hint.height = height;
_hint.flags = PPosition | PSize;
_gc = XDefaultGC(_display, _screen);
XSetBackground(_display, _gc, _bg);
XSetForeground(_display, _gc, _fg);
_visual = XDefaultVisual(_display, _screen);
_depth = XDefaultDepth(_display, _screen);
_cmap = XDefaultColormap(_display, 0);
for (i=0; i<_NCOLORS; i++) {
sprintf(_name,"gray%d",i<<1);
_stat = XAllocNamedColor(_display, _cmap,
_name,&_colors[i],
&_exact_colors[i]);
if (_stat == BadColor)
printf("ERROR: allocating color %3d\n",i);
}
XInstallColormap(_display, _cmap);
_initX = 1;
}
static
unsigned long map_pixel(x)
unsigned long x;
{
return (_colors[x].pixel);
}
static
unsigned long dither_pixel(x)
unsigned long x;
{
double d;
d = (double)x / 256.0;
d *= (double)_NCOLORS;
d = floor(d);
return map_pixel((unsigned long)d);
}
void create_X_window(_w, image, width, height)
Window *_w;
unsigned char *image;
int width, height;
{
char *data;
XImage *_image;
int i,j;
XSetWindowAttributes _xswa;
if (!_initX)
init_X(width, height);
*_w = XCreateSimpleWindow(_display, DefaultRootWindow(_display),
_hint.x, _hint.y,
_hint.width, _hint.height,
_borderwidth, _fg, _bg);
XSetStandardProperties(_display, *_w, _windowname, _iconname, None,
NULL, 0, &_hint);
_xswa.backing_store = Always;
XChangeWindowAttributes(_display, *_w, CWBackingStore, &_xswa);
XMapRaised (_display, *_w);
data = (char*)malloc(width*height*sizeof(char));
_image = XCreateImage(_display, _visual, _depth, ZPixmap,
0, data, width, height, 0, width*sizeof(char));
for (i=0 ; i<height ; i++)
for (j=0 ; j<width ; j++)
#if 1
XPutPixel(_image, j, i,
dither_pixel((unsigned long)image[i*width + j]));
#else
XPutPixel(_image, j, i, map_pixel(i>>3));
#endif
XClearWindow(_display, *_w);
XPutImage(_display, *_w, _gc, _image, 0, 0, 0, 0, width, height);
}
void destroy_X_window(_w)
Window *_w;
{
XDestroyWindow(_display, *_w);
}
void destroy_X()
{
XUninstallColormap(_display, _cmap);
XFreeColormap(_display, _cmap);
XCloseDisplay(_display);
}
void print_x11_im(char *desc, char *outfilename,
int v, int w, int q, int r, int k,
int image[v][w]::[q][r])
{
register int
i, ii, j, jj, p;
Window *_win;
unsigned char *imageX;
_win = (Window *)malloc(sizeof(Window));
imageX = (unsigned char *)malloc(v*w*q*r*sizeof(unsigned char));
p = 0;
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++)
imageX[p++] = (unsigned char)image[i][j][ii][jj];
_windowname = outfilename;
_iconname = outfilename;
create_X_window(_win, imageX, w*r, v*q);
}
void print_x11_cc_orig(char *desc, char *outfilename,
int v, int w, int q, int r, int k,
int image[v][w]::[q][r])
{
register int
i, ii, j, jj, p;
register double val;
Window *_win;
unsigned char *imageX;
_win = (Window *)malloc(sizeof(Window));
imageX = (unsigned char *)malloc(v*w*q*r*sizeof(unsigned char));
p = 0;
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++) {
val = ((double)image[i][j][ii][jj]) /
((double)(v*w*q*r) + 1.0);
val *= 255.0;
val = floor(val);
imageX[p++] = (unsigned char)((int)val);
}
_windowname = outfilename;
_iconname = outfilename;
create_X_window(_win, imageX, w*r, v*q);
}
void print_x11_cc(char *desc, char *outfilename,
int v, int w, int q, int r, int k,
int image[v][w]::[q][r])
{
register int
i, ii, j, jj, p;
register double val;
Window *_win;
unsigned char *imageX;
_win = (Window *)malloc(sizeof(Window));
imageX = (unsigned char *)malloc(v*w*q*r*sizeof(unsigned char));
p = 0;
/* Instead of linear mapping of labels to [0..255], use
255 (sin (1E+7 * x) + 1.0) / 2 */
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++) {
val = sin(12345678900.0*(double)image[i][j][ii][jj]) + 1.0;
val *= (255.0 / 2.0);
val = floor(val);
imageX[p++] = (unsigned char)((int)val);
}
_windowname = outfilename;
_iconname = outfilename;
create_X_window(_win, imageX, w*r, v*q);
}
static int binArrSearch(int * searchList, int min_idx, int max_idx, int target)
{
register int mid_idx;
mid_idx = min_idx + (max_idx - min_idx) / 2;
if (max_idx < min_idx)
return(-1);
else if (target < searchList[mid_idx])
return binArrSearch(searchList, min_idx, mid_idx - 1, target);
else if (target > searchList[mid_idx])
return binArrSearch(searchList, mid_idx + 1, max_idx , target);
else
return mid_idx;
}
static inline int lookup_label(int * intArr, int arrSize, int label)
{
return ( binArrSearch(intArr, 0, arrSize-1, label) );
}
int print_x11_real_color(char *desc, char *outfilename,
int v, int w, int q, int r, int k,
int maxLabel,
int Labels[v][w]::[q][r],
int X[v][w]::[q][r])
{
register int
i, j, ii, jj,
idx, get_proc;
int n, minL, maxL,
l, t, rows, steps, p;
int RVAL = 1<<10; /* Number of buckets per loop */
int (*locLabel)[r],
(*locX)[r],
(*locRealIm)[r];
int RealIm[v][w]::[q][r];
intpair_t
RColor[PROCS]::[RVAL],
Xpose[PROCS]::[RVAL],
*locRColor,
*locXpose;
Window *_win;
unsigned char *imageX;
on_one {
_win = (Window *)malloc(sizeof(Window));
imageX = (unsigned char *)malloc(v*w*q*r*sizeof(unsigned char));
}
ii = mapRow(v,w,MYPROC);
jj = mapCol(v,w,MYPROC);
locX = tolocal(X[ii][jj]);
locLabel = tolocal(Labels[ii][jj]);
locRealIm = tolocal(RealIm[ii][jj]);
locRColor = tolocal(RColor[MYPROC]);
locXpose = tolocal(Xpose[MYPROC]);
l=0;
n = v*w*q*r;
rows = DIVPROCS(RVAL);
steps = maxLabel / RVAL;
if (steps*RVAL != maxLabel)
steps++;
barrier();
for (t=0 ; t<steps ; t++) {
for (i=0 ; i<RVAL ; i++) {
locRColor[i].a = 0;
locRColor[i].b = 0;
}
minL = t*RVAL;
maxL = (t+1)*RVAL;
for (i=0 ; i < q ; i++)
for (j=0 ; j < r ; j++)
if ((locLabel[i][j] >= minL)&&(locLabel[i][j] < maxL)) {
idx = locLabel[i][j] % RVAL;
locRColor[idx].a++;
locRColor[idx].b += locX[i][j];
}
barrier();
for (i=0 ; i<PROCS ; i++) {
/* Stagger prefetches using MYPROC */
get_proc = (MYPROC + i) % PROCS;
bulk_read(&locXpose[rows*get_proc],
&(RColor[get_proc][rows*MYPROC]),
rows*sizeof(intpair_t));
barrier();
}
for (i=0 ; i<rows ; i++)
for (j=1 ; j<PROCS ; j++) {
locXpose[j*rows + i].a += locXpose[(j-1)*rows + i].a;
locXpose[j*rows + i].b += locXpose[(j-1)*rows + i].b;
}
barrier();
for (i=0 ; i<rows ; i++)
if (locXpose[(PROCS-1)*rows + i].a > 0)
locRColor[(MYPROC*rows) + i].a =
locXpose[(PROCS-1)*rows + i].b /
locXpose[(PROCS-1)*rows + i].a;
else
locRColor[(MYPROC*rows) + i].a = 0;
barrier();
for (i=1 ; i<PROCS ; i++) {
/* Stagger prefetches using MYPROC */
get_proc = (MYPROC + i) % PROCS;
bulk_read(&locRColor[rows*get_proc],
&(RColor[get_proc][rows*get_proc]),
rows*sizeof(intpair_t));
barrier();
}
on_one
for (i=0; i<RVAL ; i++)
if (locRColor[i].a > 0)
l++;
/* Color output */
for (i=0 ; i < q ; i++)
for (j=0 ; j < r ; j++)
if ((locLabel[i][j] >= minL)&&(locLabel[i][j] < maxL)) {
idx = locLabel[i][j] % RVAL;
locRealIm[i][j] = locRColor[idx].a;
}
}
barrier();
on_one {
p = 0;
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++)
imageX[p++] = (unsigned char)RealIm[i][j][ii][jj];
_windowname = outfilename;
_iconname = outfilename;
create_X_window(_win, imageX, w*r, v*q);
}
barrier();
return(1);
}
int print_x11_real_color_on_one(char *desc, char *outfilename,
int v, int w, int q, int r, int k,
int Labels[v][w]::[q][r],
int X[v][w]::[q][r])
{
register int
i, ii, j, jj,
idx, count;
int n, l, num, p;
int* mapLabel;
int* mapColor;
edge_t *colorArr;
register double sum;
Window *_win;
unsigned char *imageX;
_win = (Window *)malloc(sizeof(Window));
imageX = (unsigned char *)malloc(v*w*q*r*sizeof(unsigned char));
n = v*w*q*r;
if ((colorArr = (edge_t *)malloc(n*sizeof(edge_t)))==NULL)
fprintf(stderr,"ERROR: cannot malloc colorArr\n");
if ((mapColor = (int *)malloc(n*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: cannot malloc mapColor\n");
if ((mapLabel = (int *)malloc(n*sizeof(int)))==NULL)
fprintf(stderr,"ERROR: cannot malloc mapLabel\n");
idx = 0;
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++) {
colorArr[idx].a := Labels[i][j][ii][jj];
colorArr[idx].b := X[i][j][ii][jj];
idx++;
}
sync();
fastsort_edge(colorArr, idx);
l = 0;
count = 0;
sum = 0.0;
for (i=0 ; i<idx-1 ; i++) {
count++;
sum += (double)colorArr[i].b;
if (colorArr[i].a != colorArr[i+1].a) {
mapLabel[l] = colorArr[i].a;
mapColor[l] = (int)floor(sum / (double)count);
#if 0
fprintf(stdout,"(%5d) Mapping Label %4d to color %3d\n",
l,mapLabel[l], mapColor[l]);
#endif
l++;
count = 0;
sum = 0.0;
}
}
count++;
sum += colorArr[idx-1].b;
mapLabel[l] = colorArr[idx-1].a;
mapColor[l] = (int)floor(sum / (double)count);
l++;
p = 0;
for (i=0 ; i<v ; i++)
for (ii=0 ; ii < q ; ii++)
for (j=0 ; j<w ; j++)
for (jj=0 ; jj < r ; jj++) {
num = lookup_label(mapLabel,l,Labels[i][j][ii][jj]);
imageX[p++] = (unsigned char)mapColor[num];
}
_windowname = outfilename;
_iconname = outfilename;
create_X_window(_win, imageX, w*r, v*q);
free(mapLabel);
free(mapColor);
free(colorArr);
return(l);
}
#endif