download viewslice.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 408
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment 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.

    MultiSlice RTP Environment  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 MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


#include <stdio.h>
#include <string.h>            /* ANSI standard string library */
#include <stdlib.h>            /* Prototype of "calloc"        */

#include <X11/Xlib.h>
#include <X11/Xatom.h>         /* For definition of XA_WINDOW  */

#include "xwins.h"
#include "app_glob.c"


/* Scaling factor for colormap entries */
#define  CSCALE 255    

typedef char     *SCANLINE;
typedef SCANLINE *PLANE;

unsigned char  nplanes = 1;
unsigned short image_width  = 512,
               image_height = 512;
PLANE          *image = NULL;
int            bits_per_pixel = 8;
unsigned short bytes_per_line = 512;
unsigned char  bits_per_pixel_per_plane = 8;

/* Screen, visual and colormap */
int         theScreen;
Visual      *theVisual;
XVisualInfo *vis_list;
int         num_visuals,
            vis_depth;
int         colormap_size = 256;
Colormap    theColormap;

static GC   image_gc;

/* Image to be displayed by X server */
XImage         *theXImage = NULL;
unsigned char  *image_data = NULL;
XColor         *colors = NULL;
Pixmap         thePixmap;

/* File to be opened */
char *file_name;

/* Variables for the "quit" button */
static  char     *quit_label = "Quit";
static  unsigned quit_width, quit_height;

/* Function prototypes */

void initapp(/* int, char **, int, int, unsigned, unsigned */);
static int  quit_action(/* caddr_t */);
static void process_event();

static void swap2bytes(/* unsigned short *x */);
static int  adjust_index(/* int *bi, int *li, int *pi */);

static void load_file(/* char *filename */);
static void setup_disp();
static int  pick_visual(/* int depth_wanted, int class_wanted */);
static void setup_ximage();

/* Windows in the application */
Window dWin;

/*-------------------------------------------------------------*/
main(argc, argv)
int argc;
char **argv;
{
    XWIN                 *QuitButton, *which_xwin;
    XGCValues            xgcv;
    XSetWindowAttributes xswa;
    int                  i, win_depth;
    Atom                 ATOM_WM_COLMAP_WIN;
    
    xwin_init();
        
    initapp(argc, argv, 20, 20, 500, 400);

/* Get file name from command line */    
    for (i = 1; i < argc; i += 2) 
    {
        if(strcmp("-slice", argv[i]) == 0)
        {
            file_name = argv[i+1];
            break;
        }
    }
    if(i >= argc)
    {
        fprintf(stderr, "This file cannot be run from the command line.\n");
        exit(1);
    }

/* Load the image file */
    load_file(file_name);
    
/* Exit if we are not prepared to handle the image */
/* We only handle 1, 4, or 8-bit images */

    if(bits_per_pixel != 1 &&
       bits_per_pixel != 4 &&
       bits_per_pixel != 8) 
    {
        fprintf(stderr, "Cannot handle %d bits per pixel\n",
                bits_per_pixel);
        exit(1);
    }

/* Select a Visual and colormap for displaying the image */
    setup_disp();
    
/* Create the "Quit" button */
    quit_width = XTextWidth(theFontStruct, quit_label, 
                            strlen(quit_label)) + 4;
    quit_height = theFontStruct->max_bounds.ascent + 
                  theFontStruct->max_bounds.descent + 4;

    QuitButton = MakeXButton(1, 1, quit_width, quit_height,
                             1, theFGpix+1, theBGpix+5, theMain, 
                             quit_label, quit_action, NULL);

/* Create and map a window for displaying the image */
    if(bits_per_pixel == 1) 
        win_depth = CopyFromParent;
    else 
        win_depth = vis_depth;

    xswa.colormap = theColormap;
    xswa.background_pixel = theBGpix;
    xswa.border_pixel = theFGpix;

    dWin = XCreateWindow(theDisplay, theMain, 1,
           quit_height+8, image_width, image_height, 1, 
           win_depth, InputOutput, theVisual,
           CWColormap | CWBackPixel | CWBorderPixel,
           &xswa);

    XSelectInput(theDisplay, dWin, ExposureMask);

    XMapRaised(theDisplay, dWin);

/* Set the WM_COLORMAP_WINDOWS property so that the Motif 
 * window manager knows about the windows that have their
 * own colormap. In this case, we have only one such window.
 */
    if(theColormap != DefaultColormap(theDisplay, theScreen))
    {
        ATOM_WM_COLMAP_WIN = XInternAtom(theDisplay, 
                               "WM_COLORMAP_WINDOWS", False);
        XChangeProperty(theDisplay, theMain, ATOM_WM_COLMAP_WIN,
                        XA_WINDOW, 32, PropModeReplace,
                        (unsigned char *)&dWin, 1);
    }
        
/* Set up a GC to display image */
    image_gc = XCreateGC(theDisplay, dWin, 0, 0);
    XSetForeground(theDisplay, image_gc, theFGpix);
    XSetBackground(theDisplay, image_gc, theBGpix);

/* Prepare the image */
    setup_ximage();

/* Event handling loop--keep processing events until done */

    while (!AppDone)
        process_event();

/* Close connection to display and exit */

    XCloseDisplay(theDisplay);
    exit(0);
}
/*-------------------------------------------------------------*/
/*  p r o c e s s _ e v e n t 
 *  
 *  Retrieve an event and process it...
 */
static void process_event()
{
    XWIN     *which_xwin;

/* Get the next event from Xlib */
    XNextEvent(theDisplay, &theEvent);

/* Handle events for the dWin, the text display window */
    if(theEvent.xany.window == dWin)
    {
        switch (theEvent.type)
        {
            case Expose:
            {
/* Copy the exposed area from the pixmap to the window */
                XCopyArea(theDisplay, thePixmap, dWin, image_gc,
                    theEvent.xexpose.x, theEvent.xexpose.y,
                    theEvent.xexpose.width, 
                    theEvent.xexpose.height,
                    theEvent.xexpose.x, theEvent.xexpose.y);
                break;
            }
        }
    }
/************** Process events for other windows  *************/
/* Retrieve the XWIN data from Xlib using the context 
 * management routine XFindContext
 */
    if(XFindContext(theDisplay, theEvent.xany.window,
           xwin_context, (caddr_t *) &which_xwin) == 0)
    {
/* Call the event handler of this XWIN structure */
        if (*(which_xwin->event_handler) != NULL)
            (*(which_xwin->event_handler))(which_xwin);
    }
}
/*-------------------------------------------------------------*/
/*  q u i t _ a c t i o n
 *
 *  This routine is called when a ButtonPress event occurs in
 *  the quit window.
 */
static int quit_action(data)
caddr_t data;
{
/* Set the done flag */
    AppDone = 1;
    return 1;
}

static void load_file(filename)
char *filename;
{
    FILE     *p_file;
    int      numread=0, byte, i, j, 
             byteindex, lineindex, planeindex, count;
    long     imbytes, pcx_image_size;
    int cindex;

/* Open the file */
    if((p_file = fopen(filename, "r")) == NULL)
    {
        fprintf(stderr, "Cannot open: %s\n", filename);
        exit(1);
    }


/* Allocate storage for decoding image */
    pcx_image_size = (long) nplanes *
                          (long) image_height *
                          (long) bytes_per_line;

    if((image = (PLANE *) calloc(nplanes, 
                      sizeof(PLANE))) == NULL)
    {
        fprintf(stderr,
                "Error allocating image...%d planes, %d bytes\n",
                nplanes, sizeof(PLANE));
        exit(1);
    }
    for(i = 0; i < nplanes; i++)
    {
        if((image[i] = (SCANLINE *) calloc(image_height, 
                            sizeof(SCANLINE))) == NULL)
        {
            fprintf(stderr, "Error allocating plane %d...\n", i);
            exit(1);
        }

        for(j = 0; j < image_height; j++)
        {
            if((image[i][j] = (SCANLINE) 
                      calloc(bytes_per_line, 
                             sizeof(unsigned char))) == NULL)
            {
                fprintf(stderr, 
                        "Error allocating line %d in plane %d\n", 
                        j, i);
                exit(1);
            }
        }
    }

/* Decode run-length encoded image data */
    lineindex = 0;
    byteindex = 0;
    planeindex = 0;
    imbytes = 0;
    while ((byte = getc(p_file)) != EOF)
    {
       image[planeindex][lineindex][byteindex] = byte;
       imbytes++;
       if (adjust_index(&byteindex, &lineindex, &planeindex))
          break;
       if(imbytes >= pcx_image_size) break;
    }
 
/* Create the XColor entries for the colormap */
        if((colors = (XColor *)calloc(colormap_size,
                              sizeof(XColor))) == NULL)
        {
            fprintf(stderr, "No memory for setting up colormap\n");
            exit(1);
        }
        for(cindex = 0; cindex < colormap_size; cindex++)
        {
                colors[cindex].red       = CSCALE * cindex;
                colors[cindex].green     = CSCALE * cindex;
                colors[cindex].blue      = CSCALE * cindex;
                colors[cindex].pixel     = cindex;
                colors[cindex].flags     = DoRed | DoGreen | DoBlue;
        }

/* Close file and return */
    fclose(p_file);
}

/*-------------------------------------------------------------*/
/*  a d j u s t _ i n d e x
 *
 *  Adjusts the indices as image data is decoded.
 *
 */
static int adjust_index(bi, li, pi)
int *bi, *li, *pi;
{
    int status = 0;
    *bi += 1;
    if(*bi == bytes_per_line)
    {
        *bi = 0;
        *pi += 1;
        if(*pi == nplanes)
        {
            *pi = 0;
            *li += 1;
            if(*li == image_height)
            {
                printf("%d raster lines read.\n", *li);
                status = 1;
            }
        }
    }
    return (status);
}
/*-------------------------------------------------------------*/
static void setup_disp()
{
    XVisualInfo vis_template;
    int         i;
    
    theScreen = DefaultScreen(theDisplay);
    
    if(bits_per_pixel != 1) 
    {
/* Get a list of all visuals for this screen */
        vis_template.screen = theScreen;
        vis_list = XGetVisualInfo(theDisplay, 
                          VisualScreenMask, &vis_template,
                          &num_visuals);
        if(num_visuals == 0)
        {
            fprintf(stderr, "No visuals found!\n");
            exit(0);
        }

/* Search for a PseudoColor visual with depth >= image depth.
 * You may want other strategies here--for visuals such
 * as "DirectColor" or for StaticColor with 8 or more
 * bit planes. 
 */
        if(!pick_visual(bits_per_pixel, PseudoColor))
        {
            fprintf(stderr, "No appropriate visual...Exiting\n");
            exit(0);
        }

/* Create the colormap */
        theColormap = XCreateColormap(theDisplay, 
                          RootWindow(theDisplay, theScreen),
                          theVisual, AllocAll);

/* Store the colors into the colormap */
         XStoreColors(theDisplay, theColormap, colors, 
                      colormap_size); 

/* Now we can release the memory used by the colors
 * because the X server already has this information.
 */
         free(colors);
    }
    else
    {
/* Image is monochrome. Handle using default visual */
        theVisual = DefaultVisual(theDisplay, theScreen);
        theColormap = DefaultColormap(theDisplay, theScreen);
        vis_depth = DefaultDepth(theDisplay, theScreen);
    }
}
/*-------------------------------------------------------------*/
/*  p i c k _ v i s u a l
 *
 *  Select a visual of appropriate "class" and "depth" from
 *  the list of visuals. Return 1 if successful, 0 if no
 *  matching visuals found.
 */
static int pick_visual(depth_wanted, class_wanted)
int depth_wanted, class_wanted;
{
    XVisualInfo *p_visinfo;
    int         i, status = 0;
    
    for(i = 0, p_visinfo = vis_list; i < num_visuals; 
        i++, p_visinfo++)
    {
        if(p_visinfo->class == class_wanted  &&
           p_visinfo->depth >= depth_wanted)
        {
            theVisual = p_visinfo->visual;
            vis_depth = p_visinfo->depth;
            status = 1;
            break;
        }
    }
    return (status);
}
/*-------------------------------------------------------------*/
/*  s e t u p _ x i m a g e
 *
 *  Yes, we already have the decoded image in memory, but now
 *  we need it in a form usable by Xlib. This function creates
 *  the image data and sets up an XImage. It creates a Pixmap
 *  in the server and copies the image over using XPutImage.
 *  Once this is done, the image can be drawn by copying from
 *  the pixmap into the window (using XCopyArea).
 * 
 *  Note that whether monochrome or not, the image is created
 *  at the depth of the selected visual.
 *
 */
static void setup_ximage()
{
    int           i, j, k, l, planeindex, select,
                  bytes_per_vis_depth, pixval_per_byte, 
                  xim_width, pixpac_count = 0, curbyte = 0, 
                  bits_copied = 0;
    unsigned long pixval;
    unsigned int  image_bytes;

/* Compute some sizes */
    bytes_per_vis_depth = (vis_depth + 7) / 8;
    pixval_per_byte = (8/vis_depth);    
    xim_width = 8 * ((image_width+7)/8);
    image_bytes = xim_width * bytes_per_vis_depth * image_height;

    if(pixval_per_byte > 1) image_bytes /= pixval_per_byte;
    
/* Allocate room for image's data */
    if((image_data = (unsigned char *) 
                     calloc(image_bytes,
                            sizeof(unsigned char) )) == NULL)
    {
        fprintf(stderr, "Error allocating room for image...\n");
        exit(1);
    }

/* Set up image data in MSBFirst, ZPixmap format */
    if(nplanes == 1 && 
       bits_per_pixel_per_plane == 8)
    {
/* Handle 8-bit images in straightforward manner */
        for(j=0; j < image_height; j++)
	{
            for(i = 0; i < image_width; i++)
	    {
                image_data[curbyte] = image[0][j][i];
                curbyte++;
	    }
            if(xim_width > image_width)
            {
                for(k = image_width; k < xim_width; k++)
                    curbyte++;
	    }
	}
    }
    else
    {
        for(j = 0; j < image_height; j++)
        {
            for(i = 0; i< (image_width+7)/8; i++)
            {
                select = 0x80;
/* The following for loop extracts bits from each byte */
                for(k = 0; k < 8; k++)
                {
/* First construct the pixel value */
                    pixval =  (image[0][j][i] & select) > 0;
    
                    if(nplanes  == 1)
                    {
/* For monochrome images, translate 1 and 0 into White and
 * Black pixel values
 */
                        if(pixval == 0)
                            pixval = theFGpix;
                        else
                            pixval = theBGpix;
                    }
                    if(nplanes  > 1)
                    {
                        for(planeindex=1; 
                            planeindex < nplanes; 
                            planeindex++)
                        {
/* Add other planes to the pixel value */
                             pixval = pixval << 1;
                             pixval |=  (image[planeindex][j][i] & 
                                        select) > 0;
                        }
                   }

/* Copy pixel value into image's data array */
                    for(l = 0; l < bytes_per_vis_depth; l++)
                    {
                        if(pixval_per_byte > 1)
                        {
/* CASE A: more than one pixel value fits in a byte */
                            image_data[curbyte] |= 
                                (pixval & 0xff) << 
                                (8 - pixpac_count * vis_depth);
printf("'");
                            pixpac_count++;
                            if(pixpac_count == pixval_per_byte)
                            {
                               pixpac_count = 0;
                                curbyte++;
                            }
                        }
                        else
                        {
/* CASE B: more than one byte per pixel value */
                            if(bits_copied + 8 <= vis_depth)
                            {
                                image_data[curbyte] = pixval & 0xff;
                                bits_copied += 8;
                            }
                            else
                            {
                                image_data[curbyte] = 
                                    (pixval & 0xff) << 
                                    (vis_depth - bits_copied);
                                bits_copied = 0;
                            }
                            pixval >>= 8;
                            curbyte++;
                        }
                    }
/* Go to next bit in current byte from each bit plane */
                    select = select >> 1;
                }
            }
        }
    }
    
/* Set up an XImage structure in ZPixmap format */
    theXImage = XCreateImage(theDisplay, theVisual, vis_depth, 
                    ZPixmap, 0, (char *)image_data, xim_width,
                    image_height, 8, 0);

    theXImage->byte_order = MSBFirst;

/* Create a Pixmap and copy image into it */
    thePixmap = XCreatePixmap(theDisplay, dWin, theXImage->width,
                    theXImage->height, vis_depth);

    XPutImage(theDisplay, thePixmap, image_gc, theXImage, 
              0, 0, 0, 0, theXImage->width, theXImage->height);

/* Now we can destroy the image because the information is in
 * the pixmap in the X server 
 */
    XDestroyImage(theXImage);
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us