home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume22 / popi / part07 < prev    next >
Text File  |  1991-08-22  |  38KB  |  1,370 lines

  1. Newsgroups: comp.sources.misc
  2. From: Rich Burridge <richb@Aus.Sun.COM>
  3. Subject:  v22i046:  popi - The Digital Darkroom, Part07/09
  4. Message-ID: <1991Aug22.153136.15915@sparky.IMD.Sterling.COM>
  5. X-Md4-Signature: 261348ffed967e3e6fa56fffea3504fd
  6. Date: Thu, 22 Aug 1991 15:31:36 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: Rich Burridge <richb@Aus.Sun.COM>
  10. Posting-number: Volume 22, Issue 46
  11. Archive-name: popi/part07
  12. Environment: Xlib, Xview, SunView
  13. Supersedes: popi: Volume 9, Issue 47-55
  14.  
  15. #! /bin/sh
  16. # 1. Remove everything above the #! /bin/sh line
  17. # 2. Save the resulting text in a file.
  18. # 3. Execute the file with /bin/sh to create the files:
  19. #    nulldev.c
  20. #    sunview.c
  21. #    x11.c
  22. #    xview.c
  23. # This archive created: Wed Aug 21 10:36:06 EST 1991
  24. #
  25. #
  26. export PATH; PATH=/bin:$PATH
  27. #
  28. if [ -f nulldev.c ]
  29. then
  30. echo shar: will not over-write existing file nulldev.c
  31. else
  32. echo shar: extracting 'nulldev.c',     2995 characters
  33. cat > nulldev.c <<'Funky_Stuff'
  34. /*LINTLIBRARY*/
  35.  
  36. /*  @(#)nulldev.c 1.2 90/12/28
  37.  *
  38.  *  Popi device driver for a null device.
  39.  *  written by Stephen Frede, Softway Pty Ltd.
  40.  *
  41.  *  Popi was originally written by Gerard J. Holzmann - AT&T Bell Labs.
  42.  *  This version is based on the code in his Prentice Hall book,
  43.  *  "Beyond Photography - the digital darkroom," ISBN 0-13-074410-7,
  44.  *  which is copyright (c) 1988 by Bell Telephone Laboratories, Inc. 
  45.  *
  46.  *  Permission is given to distribute these extensions, as long as these
  47.  *  introductory messages are not removed, and no monies are exchanged.
  48.  *
  49.  *  No responsibility is taken for any errors or inaccuracies inherent
  50.  *  either to the comments or the code of this program, but if reported
  51.  *  (see README file) then an attempt will be made to fix them.
  52.  */
  53.  
  54. #include "popi.h"
  55.  
  56. /*  These are the exportable routines used by the popi program.
  57.  *
  58.  *  disp_init(argc, argv)    - called from main at the start.
  59.  *  disp_finish()            - called from main prior to exit.
  60.  *  disp_imgstart()          - called prior to drawing an image.
  61.  *  disp_imgend()            - called after drawing an image.
  62.  *  disp_putline(lines, y)   - to draw an image scanline triple.
  63.  *  disp_getchar()           - to get the next character typed.
  64.  *  disp_prompt()            - display popi prompt and clear input buffer.
  65.  *  disp_error(errtype)      - display error message.
  66.  *  disp_percentdone(n)      - display percentage value of conversion.
  67.  */
  68.  
  69. /*ARGSUSED*/
  70. void
  71. disp_init(argc,argv)           /* called from main at the atart. */
  72. int argc;
  73. char *argv[];
  74. {
  75. }
  76.  
  77.  
  78. void
  79. disp_finish()                  /* called from main prior to exit. */
  80. {
  81. }
  82.  
  83.  
  84. void
  85. disp_imgstart()                /* called prior to drawing an image. */
  86. {
  87. }
  88.  
  89.  
  90. void
  91. disp_imgend()                  /* called after drawing an image. */
  92. {
  93. }
  94.  
  95.  
  96. void
  97. disp_putline(lines, y)        /* called to draw image scanline y. */
  98. pixel_t **lines;
  99. int y;
  100. {
  101. }
  102.  
  103.  
  104. int
  105. disp_getchar()                 /* get next user typed character. */
  106. {
  107.     return(getchar());
  108. }
  109.  
  110.  
  111. int
  112. disp_prompt()                  /* display popi prompt. */
  113. {
  114.     static char    prompt[] = "-> ";
  115.  
  116.     PRINTF(prompt);
  117.     return sizeof prompt - 1;
  118. }
  119.  
  120.  
  121. void
  122. disp_error(errtype, pos)            /* display error message. */
  123. int    errtype,
  124.     pos;
  125. {
  126.     extern int  errno;
  127.     extern char *sys_errlist[];
  128.  
  129.     if (errtype & ERR_PARSE)
  130.     {
  131.         int     i;
  132.  
  133.         for (i=1; i < pos; ++i)
  134.             PUTC('-', stderr);
  135.         PUTC('^', stderr);
  136.         PUTC('\n', stderr);
  137.     }
  138.  
  139.     FPRINTF(stderr, "%s\n", ErrBuf);
  140.     /* we assume errno hasn't been reset by the preceding output */
  141.     if (errtype & ERR_SYS)
  142.         FPRINTF(stderr, "\t(%s)\n", sys_errlist[errno]);
  143. }
  144.  
  145. void
  146. disp_percentdone(percent)
  147. int    percent;
  148. {
  149.     static int    lastpercent = 100;
  150.  
  151.     if (!Verbose)
  152.     return;
  153.     if (percent == 100)
  154.     {
  155.     printf("\r    \n");
  156.     return;
  157.     }
  158.     if (percent != lastpercent && percent % 5 == 0)
  159.     {
  160.     printf("\r%2d%% ", percent);
  161.     fflush(stdout);
  162.     lastpercent = percent;
  163.     }
  164. }
  165. Funky_Stuff
  166. len=`wc -c < nulldev.c`
  167. if [ $len !=     2995 ] ; then
  168. echo error: nulldev.c was $len bytes long, should have been     2995
  169. fi
  170. fi # end of overwriting check
  171. if [ -f sunview.c ]
  172. then
  173. echo shar: will not over-write existing file sunview.c
  174. else
  175. echo shar: extracting 'sunview.c',    10134 characters
  176. cat > sunview.c <<'Funky_Stuff'
  177. /*LINTLIBRARY*/
  178.  
  179. /*  @(#)sunview.c 1.3 91/01/03
  180.  *
  181.  *  SunView dependent graphics routines used by popi.
  182.  *  written by Rich Burridge - Sun Microsystems.
  183.  *
  184.  *  Popi was originally written by Gerard J. Holzmann - AT&T Bell Labs.
  185.  *  This version is based on the code in his Prentice Hall book,
  186.  *  "Beyond Photography - the digital darkroom," ISBN 0-13-074410-7,
  187.  *  which is copyright (c) 1988 by Bell Telephone Laboratories, Inc. 
  188.  *
  189.  *  Permission is given to distribute these extensions, as long as these
  190.  *  introductory messages are not removed, and no monies are exchanged.
  191.  *
  192.  *  No responsibility is taken for any errors or inaccuracies inherent
  193.  *  either to the comments or the code of this program, but if reported
  194.  *  (see README file) then an attempt will be made to fix them.
  195.  */
  196.  
  197. #include "popi.h"
  198. #include <suntool/sunview.h>
  199. #include <suntool/canvas.h>
  200. #include <suntool/panel.h>
  201. #include <suntool/tty.h>
  202.  
  203. Canvas canvas ;
  204. Cursor busy_cursor, main_cursor ;
  205. Frame cframe, tframe ;
  206. Icon popi_icon ;
  207. Notify_value destroy_proc() ;
  208. Panel panel ;
  209. Panel_item slider ;
  210. Pixrect *pr ;                       /* Offscreen image area. */
  211. Pixwin *cpw ;
  212. Tty ttysw ;
  213.  
  214. short busy_cursor_array[] = {
  215. #include <images/hglass.cursor>
  216. } ;
  217. mpr_static(busy_cursor_pr, 16, 16, 1, busy_cursor_array) ;
  218.  
  219. static int depth ;          /* 1, 8, or 32 */
  220.  
  221. unsigned short icon_image[] = {
  222. #include "popi.icon"
  223. } ;
  224. mpr_static(icon_pr, 64, 64, 1, icon_image) ;
  225.  
  226. extern int errno ;
  227. extern char *sys_errlist[] ;
  228.  
  229. void canvas_proc() ;
  230.  
  231. char ttybuf[MAXLINE] ;    /* Input from user in ttysw window. */
  232. unsigned char *mptr ;     /* Pointer to scanline data. */
  233.  
  234. int iscolor ;             /* Set if this is a color screen. */
  235. int opercent = -1 ;       /* Previous display percent done value. */
  236. int tbufptr ;             /* Current pointer into ttybuf. */
  237.  
  238. /*  These are the exportable routines used by the popi program.
  239.  *
  240.  *  disp_init(argc, argv)    - called from main at the start.
  241.  *  disp_finish()            - called from main prior to exit.
  242.  *  disp_imgstart()          - called prior to drawing an image.
  243.  *  disp_imgend()            - called after drawing an image.
  244.  *  disp_putline(lines, y)   - to draw an image scanline triple.
  245.  *  disp_getchar()           - to get the next character typed.
  246.  *  disp_prompt()            - display popi prompt and clear input buffer.
  247.  *  disp_error(errtype, pos) - display error message.
  248.  *  disp_percentdone(n)      - display percentage value of conversion.
  249.  */
  250.  
  251.  
  252. void
  253. disp_init(argc, argv)        /* Called from main at the start. */
  254. int argc ;
  255. char *argv[] ;
  256. {
  257.   char title[MAXLINE] ;      /* Used to constructure window title lines. */
  258.   int cht ;                  /* Height of the popi term window. */
  259.   int cx ;                   /* X position of the popi term window. */
  260.   int cy ;                   /* Y position of the popi term window. */
  261.   int ttyfd ;                /* File descriptor for tty subwindow. */
  262.  
  263.   STRCPY(ttybuf, "") ;       /* Zeroise tty input buffer. */
  264.   tbufptr = 0 ;              /* Reset tty buffer pointer. */
  265.  
  266.   if (getenv("WINDOW_PARENT") == NULL)
  267.     {
  268.       FPRINTF(stderr,"%s: Not a native SunView window\n", ProgName) ;
  269.       exit(1) ;
  270.     }
  271.  
  272.   popi_icon = icon_create(ICON_IMAGE, &icon_pr, 0) ;     /* Create icon. */
  273.   SPRINTF(title, "%s commands", ProgName) ;
  274.   tframe = window_create((Window) 0,      FRAME,    /* Create tty frame. */
  275.                         FRAME_ICON,       popi_icon,
  276.                         WIN_ROWS,         10,
  277.                         WIN_COLUMNS,      80,
  278.                         FRAME_LABEL,      title,
  279.                         FRAME_ARGS,       argc, argv,
  280.                         0) ;
  281.   ttysw = window_create(tframe,          TERM,     /* Create tty window. */
  282.                         TTY_ARGV,        TTY_ARGV_DO_NOT_FORK,
  283.                         0) ;
  284.  
  285.   cx = (int) window_get(tframe, WIN_X) ;
  286.   cy = (int) window_get(tframe, WIN_Y) ;
  287.   cht = (int) window_get(tframe, WIN_HEIGHT) ;
  288.   SPRINTF(title, "%s image canvas", ProgName) ;
  289.   cframe = window_create((Window) 0,       FRAME,  /* Create canvas frame. */
  290.                          WIN_X,            cx,
  291.                          WIN_Y,            cy + cht + 10,
  292.                          FRAME_LABEL,      title,
  293.                          FRAME_SHOW_LABEL, TRUE,
  294.                          0) ;
  295.   panel = window_create(cframe,   PANEL,    /* Create panel for slider. */
  296.                         WIN_SHOW, FALSE,
  297.                         0) ;
  298.   slider = panel_create_item(panel, PANEL_SLIDER,
  299.                              PANEL_LABEL_STRING, "% done",
  300.                              PANEL_MIN_VALUE,    0,
  301.                              PANEL_MAX_VALUE,    100,
  302.                              0) ;
  303.   canvas = window_create(cframe,             CANVAS,     /* Create canvas. */
  304.                          WIN_WIDTH,          Xsize,
  305.                          WIN_HEIGHT,         Ysize,
  306.                          CANVAS_RETAINED,    TRUE,
  307.                          WIN_EVENT_PROC,     canvas_proc,
  308.                          0) ;
  309.   cpw = canvas_pixwin(canvas) ;
  310.   depth = cpw->pw_pixrect->pr_depth;
  311.   window_fit(cframe) ;
  312.  
  313.   (void) signal(SIGHUP, SIG_IGN) ;
  314.   ttyfd = (int) window_get(ttysw, TTY_TTY_FD) ;
  315.   (void) dup2(ttyfd, 0) ;
  316.   (void) dup2(ttyfd, 1) ;
  317.   (void) dup2(ttyfd, 2) ;
  318.  
  319.   (void) window_set(canvas, WIN_INPUT_DESIGNEE,
  320.                      (int) window_get(ttysw, WIN_DEVICE_NUMBER), 0) ;
  321.  
  322.   main_cursor = window_get(canvas, WIN_CURSOR) ;
  323.   busy_cursor = cursor_create(CURSOR_IMAGE, &busy_cursor_pr, 0) ;
  324.   load_colors() ;            /* Load the popi grayscale colormap. */
  325.  
  326.   (void) notify_interpose_destroy_func(cframe, destroy_proc) ;
  327.   (void) notify_interpose_destroy_func(tframe, destroy_proc) ;
  328.   (void) window_set(tframe, WIN_SHOW, TRUE, 0) ;
  329.   (void) window_set(cframe, WIN_SHOW, TRUE, 0) ;
  330.   (void) notify_dispatch() ;      /* Make the window appear. */
  331.   (void) notify_do_dispatch() ;
  332.   init_dither() ;    /* Initialise dither arrays and variables. */
  333. }
  334.  
  335.  
  336. void
  337. disp_finish()    /* Called from main prior to exit - null routine. */
  338. {}
  339.  
  340.  
  341.  
  342. void
  343. disp_imgstart()              /* Called prior to drawing an image. */
  344. {
  345.   (void) window_set(cframe, WIN_SHOW, TRUE, 0) ;
  346.   (void) pw_writebackground(cpw, 0, 0, Xsize, Ysize, PIX_CLR) ;
  347.   (void) window_set(panel, WIN_SHOW, FALSE, 0) ;
  348.   (void) notify_dispatch() ;
  349. }
  350.  
  351.  
  352. void
  353. disp_imgend()                /* Called after drawing an image. */
  354. {}
  355.  
  356.  
  357. void
  358. disp_putline(lines, y)        /* Draw an image scanline. */
  359. unsigned char **lines ;
  360. int y ;
  361. {
  362.   int color, i, j, x ;
  363.   unsigned char *line, tmp;
  364.  
  365.   mptr = (unsigned char *) ((struct mpr_data *) pr->pr_data)->md_image ;
  366.   if (depth == 32)
  367.   {
  368.       if (colors == 1)
  369.           for (i = j = 0; i < Xsize; i++)
  370.           {
  371.               tmp       = lines[0][i] ;
  372.               mptr[j++] = 0;                  /* X */
  373.               mptr[j++] = tmp;                /* B */
  374.               mptr[j++] = tmp;                /* G */
  375.               mptr[j++] = tmp;                /* R */
  376.           }  
  377.         else
  378.           for (i = j = 0; i < Xsize; i++)
  379.           {
  380.               mptr[j++] = 0;                  /* X */
  381.               for (color = 2; color >= 0; color--)
  382.                   mptr[j++] = lines[color][i] ; /* G, B, R */
  383.           }  
  384.   }
  385.   else if (depth == 8)
  386.   {
  387.       line = ntsc_luma(lines);
  388.       for (x = 0; x < Xsize; x++)
  389.           mptr[x] = 255 - line[x] ;
  390.   }
  391.   else
  392.   {
  393.       line = ntsc_luma(lines);
  394.       halftone(line, y) ;
  395.   }
  396.   (void) pw_rop(cpw, 0, y, Xsize, 1, PIX_SRC, pr, 0, 0) ;
  397. }
  398.  
  399.  
  400. disp_getchar()                /* Get next user typed character. */
  401. {
  402.   int c ;
  403.  
  404.   if (!tbufptr)                 /* If buffer empty, get next line. */
  405.     if (gets(ttybuf) == NULL) {    /* fake up 'q' for ^D... */
  406.       ttybuf[0] = 'q';
  407.       ttybuf[1] = 0;
  408.     }
  409.   c = ttybuf[tbufptr++] ;  
  410.   if (c == '\0')                       /* Is it end of input buffer? */
  411.     {
  412.       c = '\n' ;
  413.       tbufptr = 0 ;                    /* Reset to force another read. */
  414.       set_cursor(BUSY_CUR) ;           /* We will now get busy! */
  415.     }
  416.   return c ;
  417. }
  418.  
  419.  
  420. disp_prompt()        /* Display popi prompt and clear input line. */
  421. {
  422.   char *prompt = "\r-> " ;
  423.  
  424.   set_cursor(NORMAL_CUR) ;
  425.   PRINTF(prompt) ;
  426.   FFLUSH(stdout) ;
  427.   return(sizeof prompt - 1) ;
  428. }
  429.  
  430.  
  431. void
  432. disp_error(errtype, pos)    /* Display error message. */
  433. int errtype ;
  434. int pos ;
  435. {
  436.   int i ;
  437.  
  438.   if (errtype & ERR_PARSE)
  439.     {
  440.       for (i = 1; i < pos; ++i) PUTC('-', stderr) ;
  441.       PUTC('^', stderr) ;
  442.       PUTC('\n', stderr) ;
  443.     }
  444.  
  445.   FPRINTF(stderr, "%s\n", ErrBuf) ;
  446.  
  447. /* We assume errno hasn't been reset by the preceding output */
  448.  
  449.   if (errtype & ERR_SYS)
  450.     FPRINTF(stderr, "\t(%s)\n", sys_errlist[errno]) ;
  451.   FFLUSH(stderr) ;
  452. }
  453.  
  454.  
  455. void
  456. disp_percentdone(percent)
  457. int percent ;
  458. {
  459.   if (percent == opercent) return ;   /* Same as last time? */
  460.   panel_set_value(slider, percent) ;
  461.   if (!percent) (void) window_set(panel, WIN_SHOW, TRUE, 0) ;
  462.   opercent = percent ;
  463.   (void) notify_dispatch() ;
  464. }
  465.  
  466.  
  467. /*ARGSUSED*/
  468. void
  469. canvas_proc(window, event)
  470. Window window ;
  471. Event *event ;
  472. {
  473.   window_default_event_proc(canvas, event, (char *) 0) ;
  474. }
  475.  
  476.  
  477. /*ARGSUSED*/
  478. Notify_value
  479. destroy_proc(client, status)
  480. Notify_client client ;
  481. Destroy_status status ;
  482. {
  483.   exit(0) ;
  484. }
  485.  
  486.  
  487. load_colors()    /* Create and load popi color map. */
  488. {
  489.   int i ;
  490.   char cmsname[MAXLINE] ;
  491.   u_char red[CMAPLEN], green[CMAPLEN], blue[CMAPLEN] ;
  492.  
  493.   iscolor = (depth > 1) ? 1 : 0 ;
  494.  
  495.   if (depth == 32)
  496.       pr = mem_create(Xsize, 1, 32) ;
  497.   else if (depth == 8)
  498.   {
  499.       for (i = 0; i < CMAPLEN; i++)
  500.          red[i] = green[i] = blue[i] = 255 - i ;
  501.  
  502.       SPRINTF(cmsname, "popi%10ld", getpid()) ;
  503.       (void) pw_setcmsname(cpw, cmsname) ;
  504.       (void) pw_putcolormap(cpw, 0, CMAPLEN, red, green, blue) ;
  505.  
  506.       pr = mem_create(Xsize, 1, 8) ;
  507.   }
  508.   else
  509.       pr = mem_create(Xsize, 1, 1) ;
  510. }
  511.  
  512.  
  513. set_cursor(type)
  514. enum cur_type type ;
  515. {
  516.   switch (type)
  517.     {
  518.         case BUSY_CUR  :
  519.             (void) window_set(canvas, WIN_CURSOR, busy_cursor, 0) ;
  520.             break ;
  521.         case NORMAL_CUR:
  522.             (void) window_set(canvas, WIN_CURSOR, main_cursor, 0) ;
  523.     }
  524. }
  525. Funky_Stuff
  526. len=`wc -c < sunview.c`
  527. if [ $len !=    10134 ] ; then
  528. echo error: sunview.c was $len bytes long, should have been    10134
  529. fi
  530. fi # end of overwriting check
  531. if [ -f x11.c ]
  532. then
  533. echo shar: will not over-write existing file x11.c
  534. else
  535. echo shar: extracting 'x11.c',    10182 characters
  536. cat > x11.c <<'Funky_Stuff'
  537. /*LINTLIBRARY*/
  538.  
  539. /*  @(#)x11.c 1.5 91/08/21
  540.  *
  541.  *  X11 dependent graphics routines used by popi.
  542.  *  written by Rich Burridge - Sun Microsystems.
  543.  *
  544.  *  Popi was originally written by Gerard J. Holzmann - AT&T Bell Labs.
  545.  *  This version is based on the code in his Prentice Hall book,
  546.  *  "Beyond Photography - the digital darkroom," ISBN 0-13-074410-7,
  547.  *  which is copyright (c) 1988 by Bell Telephone Laboratories, Inc. 
  548.  *
  549.  *  Permission is given to distribute these extensions, as long as these
  550.  *  introductory messages are not removed, and no monies are exchanged.
  551.  *
  552.  *  No responsibility is taken for any errors or inaccuracies inherent
  553.  *  either to the comments or the code of this program, but if reported
  554.  *  (see README file) then an attempt will be made to fix them.
  555.  */
  556.  
  557. #include "popi.h"
  558. #include "graphics.h"
  559. #include <X11/Xlib.h>
  560. #include <X11/Xutil.h>
  561. #include <X11/cursorfont.h>
  562. #include <X11/keysym.h>
  563.  
  564. #define  BOLDFONT    "lucidasanstypewriter-bold-12"
  565. #define  DEFFONT     "fixed"
  566. #define  NORMALFONT  "lucidasanstypewriter-12"
  567.  
  568. #define  POPI_BORDER_WIDTH  2
  569.  
  570. #define  FRAME_MASK  (KeyPressMask | KeyReleaseMask | ExposureMask)
  571.  
  572. short icon_image[] = {
  573. #include "popi.icon"
  574. } ;
  575.  
  576. Atom protocol_atom, kill_atom ;
  577. Cursor busy_cursor, main_cursor ;
  578. Display *dpy ;
  579. GC gc, pix_gc ;
  580. Pixmap memarea, popi_icon, load_icon() ;
  581. Visual *visual ;
  582. Window frame, frame_icon ;
  583. XColor ccol ;
  584. XEvent event ;
  585. XFontStruct *bfont, *font, *nfont ;
  586. XGCValues gc_val ;
  587. XSetWindowAttributes attributes ;
  588. XSizeHints size ;
  589. XVisualInfo vinfo ;
  590. XWMHints wm_hints ;
  591.  
  592. unsigned long gc_mask ;
  593. int screen ;
  594. unsigned int scr_depth ;
  595. unsigned long backgnd, foregnd ;
  596. unsigned long palette[CMAPLEN] ;
  597.  
  598.  
  599. cleanup()                   /* Null routine for the X11 version. */
  600. {
  601. }
  602.  
  603.  
  604. draw_scanline(lines, y)     /* Display image scanline on the screen. */
  605. unsigned char **lines ;
  606. int y ;
  607. {
  608.   XImage *image ;
  609.   int i, len ;
  610.   unsigned char *line ;
  611.  
  612.   len = (iscolor) ? Xsize : ((Xsize / 8) + 1) ;
  613.   mptr = (unsigned char *) Emalloc(len) ;
  614.   line = ntsc_luma(lines) ;
  615.  
  616.   if (iscolor)
  617.     {
  618.       for (i = 0; i < len; i++) mptr[i] = palette[255 - line[i]] ;
  619.     }
  620.   else
  621.     {
  622.       halftone(line, y) ;
  623.     }
  624.  
  625.   image = XCreateImage(dpy, DefaultVisual(dpy, screen),
  626.                        scr_depth,
  627.                        (scr_depth > 1) ? ZPixmap : XYPixmap,
  628.                        0, mptr, Xsize, 1, 8, len) ;
  629.   XPutImage(dpy, memarea, pix_gc, image, 0, 0, 0, y+100,
  630.             (unsigned) image->width, (unsigned) image->height) ;
  631.   XDestroyImage(image) ;
  632.   XSetFunction(dpy, gc, GXcopy) ;
  633.   XCopyArea(dpy, memarea, frame, gc, 0, y+100, Xsize, 1, 0, y+100) ;
  634.   FREE(mptr) ;
  635. }
  636.  
  637.  
  638. drawarea(x, y, width, height, op)
  639. int x, y, width, height ;
  640. enum op_type op ;
  641. {
  642.   XSetFunction(dpy, gc, ops[(int) op]) ;
  643.   XFillRectangle(dpy, frame, gc, x, y,
  644.                  (unsigned int) width, (unsigned int) height) ;
  645.   XSync(dpy, 0) ;
  646. }
  647.  
  648.  
  649. drawline(x1, y1, x2, y2)
  650. int x1, y1, x2, y2 ;
  651. {
  652.   gc_val.foreground = foregnd ;
  653.   gc_val.function = GXcopy ;
  654.   XChangeGC(dpy, gc, GCForeground | GCFunction, &gc_val) ;
  655.   XDrawLine(dpy, frame, gc, x1, y1, x2, y2) ;
  656. }
  657.  
  658.  
  659. drawtext(x, y, fontno, str)
  660. enum font_type fontno ;
  661. int x, y ;
  662. char *str ;
  663. {
  664.   int len = strlen(str) ;
  665.  
  666.   if (len && str[len-1] == '\n') len-- ;
  667.  
  668.        if (fontno == NFONT) font = nfont ;
  669.   else if (fontno == BFONT) font = bfont ;
  670.   gc_val.font = font->fid ;
  671.   gc_val.function = GXset ;
  672.   XChangeGC(dpy, gc, GCFont | GCFunction, &gc_val) ;
  673.   XDrawString(dpy, frame, gc, x, y, str, len) ;
  674. }
  675.  
  676.  
  677. XFontStruct *
  678. get_font(name)
  679. char *name ;
  680. {
  681.   XFontStruct *font ;
  682.  
  683.   if (!(font = XLoadQueryFont(dpy, name)))
  684.     if (!(font = XLoadQueryFont(dpy, DEFFONT)))
  685.       {
  686.         perror("couldn't get the default font.") ;
  687.         exit(1) ;
  688.       }
  689.   return(font) ;
  690. }
  691.  
  692.  
  693. get_next_char(c)
  694. char *c ;
  695. {
  696.   XClientMessageEvent *ev ;
  697.   XKeyPressedEvent *key_event ;
  698.   KeySym keysym ;
  699.   char chs[2] ;
  700.  
  701.   for (;;)
  702.     {
  703.       if (!XCheckMaskEvent(dpy, ExposureMask, &event))
  704.         XNextEvent(dpy, &event) ;
  705.  
  706.       switch (event.type)
  707.         {
  708.           case ClientMessage    : /* Catch ICCCM kill from WM. */
  709.  
  710.                                   ev = (XClientMessageEvent *) &event ;
  711.                                   if (ev->message_type == protocol_atom &&
  712.                                       ev->data.l[0] == kill_atom)
  713.                                     exit(0) ;
  714.                                   break ;
  715.  
  716.           case Expose           : process_expose((XExposeEvent *) &event) ;
  717.                                   break ;
  718.  
  719.           case KeyPress         : key_event = (XKeyPressedEvent *) &event ;
  720.                                   (void) XLookupString(key_event, chs, 1,
  721.                                            &keysym, (XComposeStatus *) NULL) ;
  722.                                   if (keysym == XK_Shift_L ||
  723.                                       keysym == XK_Shift_R) break ;
  724.                                   *c = chs[0] ;
  725.                                   return ;
  726.        }
  727.     }
  728. /*NOTREACHED*/
  729. }
  730.  
  731.  
  732. init_fonts()
  733. {
  734.   bfont = get_font(BOLDFONT) ;
  735.   nfont = get_font(NORMALFONT) ;
  736.   nfont_width = nfont->max_bounds.rbearing + nfont->min_bounds.lbearing ;
  737. }
  738.  
  739.  
  740. init_ws_type()
  741. {
  742.   if ((dpy = XOpenDisplay(x11_display)) == NULL)
  743.     {
  744.       FPRINTF(stderr,"%s: Couldn't open display %s\n", ProgName,
  745.               (getenv ("DISPLAY") ? getenv("DISPLAY") : x11_display)) ;
  746.       exit(1) ;
  747.     }
  748.  
  749.   screen = DefaultScreen(dpy) ;
  750.  
  751.   if (!geometry)
  752.     STRCPY(geometry, XGetDefault(dpy, ProgName, "Geometry")) ;
  753.  
  754.   foregnd   = BlackPixel(dpy, screen) ;
  755.   backgnd   = WhitePixel(dpy, screen) ;
  756.   scr_depth = DefaultDepth(dpy, screen) ;
  757.  
  758.   ops[(int) GCLR] = GXclear ;
  759.   ops[(int) GSET] = GXset ;
  760.   return 0 ;
  761. }
  762.  
  763.  
  764. load_colors()    /* Create and load popi color map. */
  765. {
  766.   int i, numcolors ;
  767.  
  768.   iscolor = 0 ;
  769.   if (DisplayCells(dpy, screen) > 2)
  770.     {
  771.       ccol.flags = DoRed | DoGreen | DoBlue ;
  772.       iscolor = 1 ;
  773.       numcolors = 0 ;
  774.       for (i = 0; i < CMAPLEN; i++)
  775.         {
  776.           ccol.red = ccol.green =
  777.                      ccol.blue = (unsigned short) ((255 - i) << 8) ;
  778.           if (XAllocColor(dpy, DefaultColormap(dpy, screen), &ccol) == True)
  779.             palette[numcolors++] = ccol.pixel ;
  780.         }
  781.       if (numcolors < 2)
  782.         {
  783.           iscolor = 0 ;
  784.           FPRINTF(stderr, "%s: cannot allocate colors.\n", ProgName) ;
  785.           return ;
  786.         }
  787.     }
  788. }
  789.  
  790.  
  791. Pixmap
  792. load_icon(sbuf)
  793. short sbuf[] ;
  794. {
  795.   char cbuf[512] ;
  796.   int i ;
  797.   GC igc ;
  798.   Pixmap pixmap ;
  799.   XImage *image ;
  800.  
  801.   for (i = 0; i < 256; i++)
  802.     {
  803.       cbuf[i*2+0] = (sbuf[i] >> 8) & 0xFF ;
  804.       cbuf[i*2+1] =  sbuf[i] & 0xFF ;
  805.     }
  806.   pixmap = XCreatePixmap(dpy, RootWindow(dpy, screen), 64, 64, 1) ;
  807.   gc_mask = GCForeground | GCBackground | GCGraphicsExposures ;
  808.   gc_val.foreground         = foregnd ;
  809.   gc_val.background         = backgnd ;
  810.   gc_val.graphics_exposures = False ;
  811.   igc   = XCreateGC(dpy, pixmap, gc_mask, &gc_val) ;
  812.   image = XCreateImage(dpy, DefaultVisual(dpy, screen), 1, XYPixmap,
  813.                        0, cbuf, 64, 64, BitmapPad(dpy), 0) ;
  814.   XPutImage(dpy, pixmap, igc, image, 0, 0, 0, 0, 64, 64) ;
  815.   XDestroyImage(image) ;
  816.   return(pixmap) ;
  817. }
  818.  
  819.  
  820. make_items(argc, argv)       /* Create icon, frame, canvas etc.. */
  821. int argc ;
  822. char *argv[] ;
  823. {
  824.   unsigned int h, w ;       /* Window dimensions. */
  825.   int flags ;
  826.   int x, y ;                /* Window position. */
  827.  
  828.   load_colors() ;
  829.   popi_icon = load_icon(icon_image) ;
  830.  
  831.   size.flags = PMinSize | PMaxSize | PPosition | PSize ;
  832.   size.x = 0 ;
  833.   size.y = 0 ;
  834.   size.max_width = size.min_width = size.width = TXsize ;
  835.   size.max_height = size.min_height = size.height = TYsize ;
  836.  
  837.   if (strlen(geometry))
  838.     {
  839.       flags = XParseGeometry(geometry, &x, &y, &w, &h) ;
  840.       if (XValue & flags)
  841.         {
  842.           if (XNegative & flags)
  843.             x = DisplayWidth(dpy, screen) + x - size.width ;
  844.             size.flags |= USPosition ;
  845.             size.x = x ;
  846.         }
  847.       if (YValue & flags)
  848.         {
  849.           if (YNegative & flags)
  850.             y = DisplayHeight(dpy, screen) + y - size.height ;
  851.             size.flags |= USPosition ;
  852.             size.y = y ;
  853.         }
  854.     }
  855.  
  856.   frame = XCreateSimpleWindow(dpy, RootWindow(dpy, screen),
  857.                               size.x, size.y, size.width, size.height,
  858.                               POPI_BORDER_WIDTH, foregnd, backgnd) ;
  859.  
  860.   memarea = XCreatePixmap(dpy, frame, size.width, size.height, scr_depth) ;
  861.  
  862.   protocol_atom = XInternAtom(dpy, "WM_PROTOCOLS", False) ;
  863.   kill_atom = XInternAtom(dpy, "WM_DELETE_WINDOW", False) ;
  864.  
  865.   XSetStandardProperties(dpy, frame, "popi", NULL, popi_icon,
  866.                          argv, argc, &size) ;
  867.  
  868.   wm_hints.icon_x      = ix ;
  869.   wm_hints.icon_y      = iy ;
  870.   wm_hints.input       = True ;
  871.   wm_hints.icon_pixmap = popi_icon ;
  872.   wm_hints.flags       = IconPositionHint | InputHint | IconPixmapHint ;
  873.   if (iconic)
  874.     {
  875.       wm_hints.initial_state = IconicState ;
  876.       wm_hints.flags |= StateHint ;
  877.     }
  878.   XSetWMHints(dpy, frame, &wm_hints) ;
  879.  
  880.   gc_mask = GCFont | GCForeground | GCBackground | GCGraphicsExposures ;
  881.   gc_val.font = nfont->fid ;
  882.   gc_val.foreground = foregnd ;
  883.   gc_val.background = backgnd ;
  884.   gc_val.graphics_exposures = False ;
  885.   gc = XCreateGC(dpy, RootWindow(dpy, screen), gc_mask, &gc_val) ;
  886.   XSetFunction(dpy, gc, GXcopy) ;
  887.   pix_gc = DefaultGC(dpy, screen) ;
  888.  
  889.   main_cursor = XCreateFontCursor(dpy, XC_top_left_arrow) ;
  890.   busy_cursor = XCreateFontCursor(dpy, XC_watch) ;
  891. }
  892.  
  893.  
  894. process_expose(event)
  895. XExposeEvent *event ;
  896. {
  897.   int doframe = 0 ;
  898.  
  899.   do
  900.     {
  901.       if (event->count == 0)
  902.         if (event->window == frame) doframe++ ;
  903.     }    
  904.   while (XCheckMaskEvent(dpy, ExposureMask, event)) ;
  905.  
  906.   if (doframe)
  907.     {
  908.       paint_canvas() ;
  909.       XSetFunction(dpy, gc, GXcopy) ;
  910.       XCopyArea(dpy, memarea, frame, gc, 0, 100, Xsize, Ysize, 0, 100) ;
  911.     }
  912. }
  913.  
  914.  
  915. set_cursor(type)
  916. enum cur_type type ;
  917. {
  918.   switch (type)
  919.     {
  920.       case BUSY_CUR   : XDefineCursor(dpy, frame, busy_cursor) ;
  921.                         break ;
  922.       case NORMAL_CUR : XDefineCursor(dpy, frame, main_cursor) ;
  923.     }
  924. }
  925.  
  926.  
  927. start_tool()
  928. {
  929.   XSelectInput(dpy, frame, FRAME_MASK) ;
  930.   XMapWindow(dpy, frame) ;
  931. }
  932. Funky_Stuff
  933. len=`wc -c < x11.c`
  934. if [ $len !=    10182 ] ; then
  935. echo error: x11.c was $len bytes long, should have been    10182
  936. fi
  937. fi # end of overwriting check
  938. if [ -f xview.c ]
  939. then
  940. echo shar: will not over-write existing file xview.c
  941. else
  942. echo shar: extracting 'xview.c',    12966 characters
  943. cat > xview.c <<'Funky_Stuff'
  944. /*LINTLIBRARY*/
  945.  
  946. /*  @(#)xview.c 1.5 91/08/20
  947.  *
  948.  *  XView dependent graphics routines used by popi.
  949.  *  written by Rich Burridge - Sun Microsystems.
  950.  *
  951.  *  Popi was originally written by Gerard J. Holzmann - AT&T Bell Labs.
  952.  *  This version is based on the code in his Prentice Hall book,
  953.  *  "Beyond Photography - the digital darkroom," ISBN 0-13-074410-7,
  954.  *  which is copyright (c) 1988 by Bell Telephone Laboratories, Inc. 
  955.  *
  956.  *  Permission is given to distribute these extensions, as long as these
  957.  *  introductory messages are not removed, and no monies are exchanged.
  958.  *
  959.  *  No responsibility is taken for any errors or inaccuracies inherent
  960.  *  either to the comments or the code of this program, but if reported
  961.  *  (see README file) then an attempt will be made to fix them.
  962.  */
  963.  
  964. #include "popi.h"
  965. #include <xview/xview.h>
  966. #include <xview/canvas.h>
  967. #include <xview/cursor.h>
  968. #include <xview/panel.h>
  969. #include <xview/svrimage.h>
  970. #include <xview/tty.h>
  971. #include <xview/termsw.h>
  972. #include <xview/xv_xrect.h>
  973. #include <X11/Xlib.h>
  974.  
  975. #define  DUP2                           (void) dup2
  976. #define  SIGNAL                         (void) signal
  977.  
  978. #define  NOTIFY_DISPATCH                (void) notify_dispatch
  979. #define  NOTIFY_DO_DISPATCH             (void) notify_do_dispatch
  980. #define  NOTIFY_INTERPOSE_DESTROY_FUNC  (void) notify_interpose_destroy_func
  981. #define  XV_SET                         (void) xv_set
  982.  
  983. Canvas canvas ;
  984. Canvas_paint_window cpw ;
  985. Frame cframe, tframe ;
  986. Icon popi_icon ;
  987. Notify_value destroy_proc() ;
  988. Panel panel ;
  989. Panel_item slider ;
  990. Server_image busy_cursor_pr, icon_pr ;
  991. Tty ttysw ;
  992. Xv_Cursor busy_cursor, main_cursor ;
  993.  
  994. Display *dpy ;                  /* Display id of popi frame. */
  995. Drawable xid ;                  /* Xid for popi canvas. */
  996. GC gc ;                         /* Graphics context for text and lines. */
  997. GC pix_gc ;                     /* Graphics context for rops. */
  998. Pixmap memarea, mpr ;
  999. Window root ;
  1000. XColor ccol ;
  1001. XGCValues gc_val ;              /* Used to setup graphics context values. */
  1002. int gc_flags ;                  /* Used to set up graphics context flags. */
  1003. int screen ;                    /* Default graphics display screen. */
  1004. unsigned int scr_depth ;
  1005. unsigned long backgnd ;         /* Default background color. */
  1006. unsigned long foregnd ;         /* Default foreground color. */
  1007. unsigned long gc_mask ;         /* Mask for setting graphic context values. */
  1008. unsigned long palette[CMAPLEN] ;
  1009.  
  1010. static unsigned short busy_cursor_array[] = {
  1011. #include <images/hglass.cursor>
  1012. } ;
  1013.  
  1014. static unsigned short icon_image[] = {
  1015. #include "popi.icon"
  1016. } ;
  1017.  
  1018. extern int errno ;
  1019. extern char *sys_errlist[] ;
  1020.  
  1021. char ttybuf[MAXLINE] ;       /* Input from user in ttysw window. */
  1022. unsigned char *mptr ;        /* Pointer to scanline data. */
  1023.  
  1024. int iscolor ;                /* Set if this is a color screen. */
  1025. int opercent = -1 ;          /* Previous display percent done value. */
  1026. int tbufptr ;                /* Current pointer into ttybuf. */
  1027.  
  1028. /*ARGSUSED*/
  1029. static void
  1030. canvas_repaint(canvas, window, display, xid, xrects)
  1031. Canvas canvas ;
  1032. Xv_Window window ;
  1033. Display *display ;
  1034. Xv_Window xid ;
  1035. Xv_xrectlist *xrects ;
  1036. {
  1037.   XCopyArea(dpy, memarea, xid, gc, 0, 0, Xsize, Ysize, 0, 00) ;
  1038. }
  1039.  
  1040.  
  1041. /*  These are the exportable routines used by the popi program.
  1042.  *
  1043.  *  disp_init(argc, argv)    - called from main at the start.
  1044.  *  disp_finish()            - called from main prior to exit.
  1045.  *  disp_imgstart()          - called prior to drawing an image.
  1046.  *  disp_imgend()            - called after drawing an image.
  1047.  *  disp_putline(lines, y)   - to draw an image scanline triple.
  1048.  *  disp_getchar()           - to get the next character typed.
  1049.  *  disp_prompt()            - display popi prompt and clear input buffer.
  1050.  *  disp_error(errtype, pos) - display error message.
  1051.  *  disp_percentdone(n)      - display percentage value of conversion.
  1052.  */
  1053.  
  1054.  
  1055. void
  1056. disp_init(argc, argv)        /* Called from main at the start. */
  1057. int argc ;
  1058. char *argv[] ;
  1059. {
  1060.   char title[MAXLINE] ;      /* Used to constructure window title lines. */
  1061.   int cht ;                  /* Height of the popi term window. */
  1062.   int cx ;                   /* X position of the popi term window. */
  1063.   int cy ;                   /* Y position of the popi term window. */
  1064.   int ttyfd ;                /* File descriptor for tty subwindow. */
  1065.  
  1066.   STRCPY(ttybuf, "") ;       /* Zeroise tty input buffer. */
  1067.   tbufptr = 0 ;              /* Reset tty buffer pointer. */
  1068.  
  1069.   xv_init(XV_INIT_ARGS, argc, argv, 0) ;
  1070.   icon_pr = xv_create(XV_NULL,            SERVER_IMAGE,
  1071.                       SERVER_IMAGE_BITS,  icon_image,
  1072.                       SERVER_IMAGE_DEPTH, 1,
  1073.                       XV_WIDTH,           64,
  1074.                       XV_HEIGHT,          64,
  1075.                       0) ;
  1076.   popi_icon = xv_create(XV_NULL,    ICON,           /* Create icon. */
  1077.                         ICON_IMAGE, icon_pr,
  1078.                         0) ;
  1079.  
  1080.   SPRINTF(title, "%s commands", ProgName) ;
  1081.   tframe = xv_create(XV_NULL,     FRAME,            /* Create tty frame. */
  1082.                      FRAME_ICON,  popi_icon,
  1083.                      WIN_ROWS,    10,
  1084.                      WIN_COLUMNS, 80,
  1085.                      FRAME_LABEL, title,
  1086.                      0) ;
  1087.  
  1088.   ttysw = xv_create(tframe,   TERMSW,
  1089.                     TTY_ARGV, TTY_ARGV_DO_NOT_FORK,
  1090.                     0) ;
  1091.   XV_SET(ttysw, TERMSW_MODE, TERMSW_MODE_TYPE, 0) ;
  1092.  
  1093.   cx  = (int) xv_get(tframe, XV_X) ;
  1094.   cy  = (int) xv_get(tframe, XV_Y) ;
  1095.   cht = (int) xv_get(tframe, XV_HEIGHT) ;
  1096.   SPRINTF(title, "%s image canvas", ProgName) ;
  1097.   cframe = xv_create(XV_NULL,          FRAME,       /* Create canvas frame. */
  1098.                      XV_X,             cx,
  1099.                      XV_Y,             cy + cht + 10,
  1100.                      FRAME_LABEL,      title,
  1101.                      FRAME_SHOW_LABEL, TRUE,
  1102.                      0) ;
  1103.   panel = xv_create(cframe,  PANEL,             /* Create panel for slider. */
  1104.                     XV_X,    0,
  1105.                     XV_Y,    0,
  1106.                     XV_SHOW, FALSE,
  1107.                     0) ;
  1108.   slider = xv_create(panel,              PANEL_SLIDER,
  1109.                      PANEL_LABEL_STRING, "% done",
  1110.                      PANEL_MIN_VALUE,    0,
  1111.                      PANEL_MAX_VALUE,    100,
  1112.                      0) ;
  1113.   canvas = window_create(cframe,                CANVAS,   /* Create canvas. */
  1114.                          XV_X,                  0,
  1115.                          XV_Y,                  0,
  1116.                          XV_WIDTH,              Xsize,
  1117.                          XV_HEIGHT,             Ysize,
  1118.                          CANVAS_X_PAINT_WINDOW, TRUE,
  1119.                          CANVAS_REPAINT_PROC,   canvas_repaint,
  1120.                          CANVAS_RETAINED,       FALSE,
  1121.                          0) ;
  1122.   cpw = canvas_paint_window(canvas) ;
  1123.   window_fit(cframe) ;
  1124.  
  1125.   dpy = (Display *) xv_get(cframe, XV_DISPLAY, NULL) ;
  1126.   xid = (Drawable) xv_get(cpw, XV_XID, NULL) ;
  1127.  
  1128.   screen    = DefaultScreen(dpy) ;
  1129.   root      = RootWindow(dpy, screen) ;
  1130.   foregnd   = BlackPixel(dpy, screen) ;
  1131.   backgnd   = WhitePixel(dpy, screen) ;
  1132.   scr_depth = DefaultDepth(dpy, screen) ;
  1133.  
  1134.   gc_mask           = GCForeground | GCBackground | GCGraphicsExposures ;
  1135.   gc_val.foreground = foregnd ;
  1136.   gc_val.background = backgnd ;
  1137.   gc_val.graphics_exposures = False ;
  1138.   gc     = XCreateGC(dpy, root, gc_mask, &gc_val) ;
  1139.   pix_gc = DefaultGC(dpy, screen) ;
  1140.  
  1141.   memarea = XCreatePixmap(dpy, xid, Xsize, Ysize, scr_depth) ;
  1142.  
  1143.   SIGNAL(SIGHUP, SIG_IGN) ;
  1144.   ttyfd = (int) xv_get(ttysw, TTY_TTY_FD) ;
  1145.   DUP2(ttyfd, 0) ;
  1146.   DUP2(ttyfd, 1) ;
  1147.   DUP2(ttyfd, 2) ;
  1148.  
  1149.   main_cursor = xv_get(canvas, WIN_CURSOR) ;
  1150.  
  1151.   busy_cursor_pr = xv_create(XV_NULL,           SERVER_IMAGE,
  1152.                              XV_WIDTH,          16,
  1153.                              XV_HEIGHT,         16,
  1154.                              SERVER_IMAGE_BITS, busy_cursor_array,
  1155.                              0) ;
  1156.  
  1157.   busy_cursor = xv_create(XV_NULL,      CURSOR,
  1158.                           CURSOR_IMAGE, busy_cursor_pr,
  1159.                           0) ;
  1160.   load_colors() ;            /* Load the popi grayscale colormap. */
  1161.  
  1162.   NOTIFY_INTERPOSE_DESTROY_FUNC(cframe, destroy_proc) ;
  1163.   NOTIFY_INTERPOSE_DESTROY_FUNC(tframe, destroy_proc) ;
  1164.   XV_SET(tframe, XV_SHOW, TRUE, 0) ;
  1165.   XV_SET(cframe, XV_SHOW, TRUE, 0) ;
  1166.   NOTIFY_DISPATCH() ;        /* Make the windows appear. */
  1167.   NOTIFY_DO_DISPATCH() ;
  1168.   init_dither() ;    /* Initialise dither arrays and variables. */
  1169. }
  1170.  
  1171.  
  1172. void
  1173. disp_finish()    /* Called from main prior to exit - null routine. */
  1174. {}
  1175.  
  1176.  
  1177.  
  1178. void
  1179. disp_imgstart()              /* Called prior to drawing an image. */
  1180. {
  1181.   XV_SET(cframe, WIN_SHOW, TRUE, 0) ;
  1182.   XClearWindow(dpy, xid) ;
  1183.   XV_SET(panel, WIN_SHOW, FALSE, 0) ;
  1184.   NOTIFY_DISPATCH() ;
  1185. }
  1186.  
  1187.  
  1188. void
  1189. disp_imgend()                /* Called after drawing an image. */
  1190. {}
  1191.  
  1192.  
  1193. void
  1194. disp_putline(lines, y)        /* Draw an image scanline. */
  1195. unsigned char **lines ;
  1196. int y ;
  1197. {
  1198.   XImage *image ;
  1199.   int color, i, j, len ;
  1200.   unsigned char *line, tmp ;
  1201.  
  1202.        if (scr_depth == 32) len = Xsize * 4 ;
  1203.   else if (scr_depth == 8)  len = Xsize ;
  1204.   else                      len = (Xsize / 8) + 1 ;
  1205.   mptr = (unsigned char *) Emalloc(len) ;
  1206.  
  1207.   if (scr_depth == 32)
  1208.     {
  1209.       if (colors == 1)
  1210.         for (i = j = 0; i < Xsize; i++)
  1211.           {
  1212.             tmp       = lines[0][i] ;
  1213.             mptr[j++] = 0 ;                           /* X */
  1214.             mptr[j++] = tmp ;                         /* B */
  1215.             mptr[j++] = tmp ;                         /* G */
  1216.             mptr[j++] = tmp ;                         /* R */
  1217.           }
  1218.         else
  1219.           for (i = j = 0; i < Xsize; i++)
  1220.             {
  1221.               mptr[j++] = 0 ;                         /* X */
  1222.               for (color = 2; color >= 0; color--)
  1223.                 mptr[j++] = lines[color][i] ;         /* G, B, R */
  1224.             }
  1225.     }
  1226.   else if (scr_depth == 8)
  1227.     {
  1228.       line = ntsc_luma(lines) ;
  1229.       for (i = 0; i < len; i++) mptr[i] = 255 - line[i] ;
  1230.     }
  1231.   else
  1232.     {
  1233.       line = ntsc_luma(lines) ;
  1234.       halftone(line, y) ;
  1235.     }
  1236.  
  1237.   image = XCreateImage(dpy, DefaultVisual(dpy, screen),
  1238.                        scr_depth,
  1239.                        (scr_depth > 1) ? ZPixmap : XYPixmap,
  1240.                        0, mptr, Xsize, 1, 8, len) ;
  1241.   XPutImage(dpy, memarea, pix_gc, image, 0, 0, 0, y,
  1242.             (unsigned) image->width, (unsigned) image->height) ;
  1243.   XDestroyImage(image) ;
  1244.   XSetFunction(dpy, gc, GXcopy) ;
  1245.   XCopyArea(dpy, memarea, xid, gc, 0, y, Xsize, 1, 0, y) ;
  1246.  
  1247.   FREE(mptr) ;
  1248.   NOTIFY_DISPATCH() ;
  1249. }
  1250.  
  1251.  
  1252. disp_getchar()                /* Get next user typed character. */
  1253. {
  1254.   int c ;
  1255.  
  1256.   if (!tbufptr) (void) gets(ttybuf) ;  /* If buffer empty, get next line. */
  1257.   c = ttybuf[tbufptr++] ;  
  1258.   if (c == '\0')                       /* Is it end of input buffer? */
  1259.     {
  1260.       c = '\n' ;
  1261.       tbufptr = 0 ;                    /* Reset to force another read. */
  1262.       set_cursor(BUSY_CUR) ;           /* We will now get busy! */
  1263.     }
  1264.   return c ;
  1265. }
  1266.  
  1267.  
  1268. disp_prompt()        /* Display popi prompt and clear input line. */
  1269. {
  1270.   char *prompt = "\r-> " ;
  1271.  
  1272.   set_cursor(NORMAL_CUR) ;
  1273.   PRINTF(prompt) ;
  1274.   FFLUSH(stdout) ;
  1275.   return(sizeof prompt - 1) ;
  1276. }
  1277.  
  1278.  
  1279. void
  1280. disp_error(errtype, pos)    /* Display error message. */
  1281. int errtype ;
  1282. int pos ;
  1283. {
  1284.   int i ;
  1285.  
  1286.   if (errtype & ERR_PARSE)
  1287.     {
  1288.       for (i = 1; i < pos; ++i) PUTC('-', stderr) ;
  1289.       PUTC('^', stderr) ;
  1290.       PUTC('\n', stderr) ;
  1291.     }
  1292.  
  1293.   FPRINTF(stderr, "%s\n", ErrBuf) ;
  1294.  
  1295. /* We assume errno hasn't been reset by the preceding output */
  1296.  
  1297.   if (errtype & ERR_SYS)
  1298.     FPRINTF(stderr, "\t(%s)\n", sys_errlist[errno]) ;
  1299.   FFLUSH(stderr) ;
  1300. }
  1301.  
  1302.  
  1303. void
  1304. disp_percentdone(percent)
  1305. int percent ;
  1306. {
  1307.   if (percent == opercent) return ;   /* Same as last time? */
  1308.   XV_SET(slider, PANEL_VALUE, percent, 0) ;
  1309.   if (!percent) XV_SET(panel, WIN_SHOW, TRUE, 0) ;
  1310.   opercent = percent ;
  1311.   NOTIFY_DISPATCH() ;
  1312. }
  1313.  
  1314.  
  1315. /*ARGSUSED*/
  1316. Notify_value
  1317. destroy_proc(client, status)
  1318. Notify_client client ;
  1319. Destroy_status status ;
  1320. {
  1321.   exit(0) ;
  1322. }
  1323.  
  1324.  
  1325. load_colors()    /* Create and load popi color map. */
  1326. {
  1327.   u_char red[CMAPLEN], green[CMAPLEN], blue[CMAPLEN] ;
  1328.   int i, numcolors ;
  1329.  
  1330.   iscolor = 0 ;
  1331.   if (DisplayCells(dpy, screen) > 2)
  1332.     {
  1333.       ccol.flags = DoRed | DoGreen | DoBlue ;
  1334.       iscolor = 1 ;
  1335.       numcolors = 0 ;
  1336.       for (i = 0; i < CMAPLEN; i++)
  1337.         {
  1338.           ccol.red = ccol.green =
  1339.                      ccol.blue = (unsigned short) ((255 - i) << 8) ;
  1340.           if (XAllocColor(dpy, DefaultColormap(dpy, screen), &ccol) == True)
  1341.             palette[numcolors++] = ccol.pixel ;
  1342.         }
  1343.       if (numcolors < 2)
  1344.         {
  1345.           iscolor = 0 ;
  1346.           FPRINTF(stderr, "%s: cannot allocate colors.\n", ProgName) ;
  1347.           return ;
  1348.         }
  1349.     }
  1350. }
  1351.  
  1352.  
  1353. set_cursor(type)
  1354. enum cur_type type ;
  1355. {
  1356.   switch (type)
  1357.     {
  1358.         case BUSY_CUR  : XV_SET(canvas, WIN_CURSOR, busy_cursor, 0) ;
  1359.                          break ;
  1360.         case NORMAL_CUR: XV_SET(canvas, WIN_CURSOR, main_cursor, 0) ;
  1361.     }
  1362. }
  1363. Funky_Stuff
  1364. len=`wc -c < xview.c`
  1365. if [ $len !=    12966 ] ; then
  1366. echo error: xview.c was $len bytes long, should have been    12966
  1367. fi
  1368. fi # end of overwriting check
  1369. exit 0 # Just in case...
  1370.