[comp.sources.x] v12i033: tgif, Part17/23

william@CS.UCLA.EDU (William Cheng) (03/14/91)

Submitted-by: william@CS.UCLA.EDU (William Cheng)
Posting-number: Volume 12, Issue 33
Archive-name: tgif/part17

---------------------------------> cut here <---------------------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 17 (of 23)."
# Contents:  tgif.c tgif2ps.c version.c xbitmap.c
# Wrapped by william@oahu on Wed Mar  6 09:57:58 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'tgif.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tgif.c'\"
else
echo shar: Extracting \"'tgif.c'\" \(3498 characters\)
sed "s/^X//" >'tgif.c' <<'END_OF_FILE'
X/*
X * Author:	William Chia-Wei Cheng (william@cs.ucla.edu)
X *
X * Copyright (C) 1990, 1991, William Cheng.
X */
X#ifndef lint
Xstatic char RCSid[] =
X      "@(#)$Header: /tmp_mnt/n/kona/tangram/u/william/X11/TGIF2/RCS/tgif.c,v 2.0 91/03/05 12:48:48 william Exp $";
X#endif
X
X#include <stdio.h>
X#include <X11/Xlib.h>
X#include "const.h"
X#include "types.h"
X
X#include "mainloop.e"
X#include "msg.e"
X#include "setup.e"
X
X/*
X * extern int	malloc_debug ();
X */
X
Xint	lastFile;
Xshort	* pDrawFontAsc;
Xshort	* pDrawFontDes;
X
X/*
X * static
X * void Prompt2 (PromptStr, OpName, FileName)
X *    char	* PromptStr, * OpName, * FileName;
X * {
X *    char	inbuf[80];
X * 
X *    printf (PromptStr);
X *    fgets (inbuf, 80, stdin);
X *    sscanf (inbuf, "%s%s", OpName, FileName);
X * }
X *
X * static
X * void Prompt3 (PromptStr, AttrName, ColorName, ValName)
X *    char	* PromptStr, * AttrName, * ColorName, * ValName;
X * {
X *    char	inbuf[80];
X * 
X *    printf (PromptStr);
X *    fgets (inbuf, 80, stdin);
X *    sscanf (inbuf, "%s%s%s", AttrName, ColorName, ValName);
X * }
X */
X
Xint main (argc, argv)
X   int	argc;
X   char	* argv[];
X   /* All these strangeness with strings are related to */
X   /*	Prolog's foreign function interface. */
X{
X   int	i;
X   char	op_name[80], file_name[80], s[80];
X   char	* sp[6], * func_strp;
X/*
X * char	color_name[80], val_name[80];
X * char	attr_name[80], speed_name[80], id_name[80];
X */
X   file_name[0] = '\0';
X   for (i = 1; i < argc; i++)
X   {
X      if (*argv[i] == '=')
X      {
X         strcpy (geometrySpec, argv[i]);
X         geometrySpecified = TRUE;
X      }
X      else if (*argv[i] != '-')
X         strcpy (file_name, argv[i]);
X   }
X/*
X * malloc_debug (1);
X */
X   if (file_name[0] == '\0')
X      MainLoop ("init", "", &func_strp,
X            &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
X   else
X      MainLoop ("init", file_name, &func_strp,
X            &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
X/*
X * for (i = 0; i < 6; i++)
X *    if (strcmp (sp[i], "") != 0)
X *       printf ("%s ", sp[i]);
X *    else
X *       break;
X * printf ("\n");
X */
X
X   while (TRUE)
X   {
X      strcpy (s, func_strp);
X      s[4] = '\0';
X
X      DeallocStrings (&func_strp,&sp[0],&sp[1],&sp[2],&sp[3],&sp[4],&sp[5]);
X
X      if (strcmp (s, "Quit") == 0)
X      {
X         MainLoop ("quit", file_name, &func_strp,
X               &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
X         break;
X      }
X
X      Msg ("Returned from basic driver.");
X/*
X *    Prompt2 ("Input an operation and a sub command.\n",op_name,file_name);
X *
X *    if (strcmp (op_name, "animate") == 0)
X *    {
X *       Prompt3 ("Input poly_id, speed, color.\n", id_name, speed_name,
X *             color_name);
X *       Animate (file_name, id_name, speed_name, color_name, &func_strp);
X *       printf ("Animate RETURNs --> %s %s %s\n", func_strp, sp[0], sp[1]);
X *    }
X *    if (strcmp (op_name, "upd_attr_val") == 0)
X *    {
X *       Prompt3 ("Input attrname, color and value.\n", attr_name, color_name,
X *             val_name);
X *       UpdAttrVal (file_name, attr_name, color_name, val_name, &func_strp);
X *       printf ("UpdAttrVal RETURNs --> %s %s %s\n", func_strp, sp[0], sp[1]);
X *    }
X */
X      MainLoop (op_name, file_name, &func_strp,
X            &sp[0], &sp[1], &sp[2], &sp[3], &sp[4], &sp[5]);
X/*
X *    printf ("RETURN --> %s ", func_strp);
X *    for (i = 0; i < 6; i++)
X *       if (strcmp (sp[i], "") != 0)
X *          printf ("%s ", sp[i]);
X *       else
X *          break;
X *    printf ("\n");
X */
X   }
X   return (0);
X}
END_OF_FILE
if test 3498 -ne `wc -c <'tgif.c'`; then
    echo shar: \"'tgif.c'\" unpacked with wrong size!
fi
# end of 'tgif.c'
fi
if test -f 'tgif2ps.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'tgif2ps.c'\"
else
echo shar: Extracting \"'tgif2ps.c'\" \(3727 characters\)
sed "s/^X//" >'tgif2ps.c' <<'END_OF_FILE'
X/*
X * Author:	William Chia-Wei Cheng (william@cs.ucla.edu)
X *
X * Copyright (C) 1990, 1991, William Cheng.
X */
X#ifndef lint
Xstatic char RCSid[] =
X      "@(#)$Header: /tmp_mnt/n/kona/tangram/u/william/X11/TGIF2/RCS/tgif2ps.c,v 2.0 91/03/05 12:48:49 william Exp $";
X#endif
X
X#include <stdio.h>
X
Xvoid Init ()
X{
X   char	inbuf[256];
X
X   if (fgets (inbuf, 255, stdin) == NULL)
X   {
X      fprintf (stderr, "ERROR:  Bad first line in input file.\n");
X      exit (-1);
X   }
X   if (strcmp (inbuf, "%!\n") != 0)
X   {
X      fprintf (stderr, "ERROR:  Bad first line in input file.\n");
X      exit (-1);
X   }
X   printf ("%%!\n");
X}
X
Xmain ()
X{
X   char	inbuf[256];
X
X   Init ();
X   while (fgets (inbuf, 255, stdin) != NULL)
X   {
X      if (strcmp (inbuf, "90 rotate\n") == 0)
X         continue;
X      else if (strcmp (inbuf, "72 0.55 mul 72 10.4 mul translate\n") == 0)
X      {  /* portrait format */
X	 if (fgets (inbuf, 255, stdin) == NULL)
X         {
X            fprintf (stderr, "ERROR:  Incomplete file.\n");
X            exit (-1);
X         }
X         if (strcmp (inbuf, "0.565 -0.565 scale\n") != 0)
X         {
X            fprintf (stderr, "ERROR:  '0.565 -0.565 scale' not found.\n");
X            exit (-1);
X         }
X         printf ("0.565 -0.565 scale\n");
X      }
X      else if (strcmp (inbuf, "72 0.28 mul 72 10.7 mul translate\n") == 0)
X      {  /* hi-portrait format */
X	 if (fgets (inbuf, 255, stdin) == NULL)
X         {
X            fprintf (stderr, "ERROR:  Incomplete file.\n");
X            exit (-1);
X         }
X         if (strcmp (inbuf, "0.28 -0.28 scale\n") != 0)
X         {
X            fprintf (stderr, "ERROR:  '0.28 -0.28 scale' not found.\n");
X            exit (-1);
X         }
X         printf ("0.28 -0.28 scale\n");
X      }
X      else if (strcmp (inbuf, "72 0.565 mul 72 10.4 mul translate\n") == 0)
X      {  /* slide-portrait format */
X	 if (fgets (inbuf, 255, stdin) == NULL)
X         {
X            fprintf (stderr, "ERROR:  Incomplete file.\n");
X            exit (-1);
X         }
X         if (strcmp (inbuf, "1.13 -1.13 scale\n") != 0)
X         {
X            fprintf (stderr, "ERROR:  '1.13 -1.13 scale' not found.\n");
X            exit (-1);
X         }
X         printf ("1.13 -1.13 scale\n");
X      }
X      else if (strcmp (inbuf, "72 0.4 mul 72 -0.6 mul translate\n") == 0)
X      {  /* landscape or slide-landscape format */
X/*       printf ("72 3.5 mul 72 -0.6 mul translate\n"); */
X	 if (fgets (inbuf, 255, stdin) == NULL)
X         {
X            fprintf (stderr, "ERROR:  Incomplete file.\n");
X            exit (-1);
X         }
X         if (strcmp (inbuf, "0.565 -0.565 scale\n") != 0)
X         {
X            if (strcmp (inbuf, "1.13 -1.13 scale\n") != 0)
X            {
X               fprintf (stderr, "ERROR:  neither '0.565 -0.565 scale' nor ");
X               fprintf (stderr, "'1.13 -1.13 scale' is found.\n");
X               exit (-1);
X            }
X            printf ("1.13 -1.13 scale\n");
X         }
X         else
X            printf ("0.565 -0.565 scale\n");
X      }
X      else if (strcmp (inbuf, "72 0.2 mul 72 -0.3 mul translate\n") == 0)
X      {  /* hi-landscape format */
X/*       printf ("72 1.75 mul 72 -0.3 mul translate\n"); */
X	 if (fgets (inbuf, 255, stdin) == NULL)
X         {
X            fprintf (stderr, "ERROR:  Incomplete file.\n");
X            exit (-1);
X         }
X         if (strcmp (inbuf, "0.28 -0.28 scale\n") != 0)
X         {
X            fprintf (stderr, "ERROR:  '0.28 -0.28 scale' not found.\n");
X            exit (-1);
X         }
X         printf ("0.28 -0.28 scale\n");
X      }
X      else if (strcmp (inbuf, "showpage\n") == 0)
X      {
X         exit (0);
X      }
X      else
X         printf ("%s", inbuf);
X   }
X   fprintf (stderr, "ERROR:  Incomplete file.\n");
X   exit (-1);
X}
END_OF_FILE
if test 3727 -ne `wc -c <'tgif2ps.c'`; then
    echo shar: \"'tgif2ps.c'\" unpacked with wrong size!
fi
# end of 'tgif2ps.c'
fi
if test -f 'version.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'version.c'\"
else
echo shar: Extracting \"'version.c'\" \(302 characters\)
sed "s/^X//" >'version.c' <<'END_OF_FILE'
X/*
X * Author:	William Chia-Wei Cheng (william@cs.ucla.edu)
X *
X * Copyright (C) 1990, 1991, William Cheng.
X */
X#ifndef lint
Xstatic char RCSid[] =
X      "@(#)$Header: /tmp_mnt/n/kona/tangram/u/william/X11/TGIF2/RCS/version.c,v 2.1 91/03/06 09:35:30 william Exp $";
X#endif
X
Xchar	* version_string = "2.1";
END_OF_FILE
if test 302 -ne `wc -c <'version.c'`; then
    echo shar: \"'version.c'\" unpacked with wrong size!
fi
# end of 'version.c'
fi
if test -f 'xbitmap.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'xbitmap.c'\"
else
echo shar: Extracting \"'xbitmap.c'\" \(25875 characters\)
sed "s/^X//" >'xbitmap.c' <<'END_OF_FILE'
X/*
X * Author:	William Chia-Wei Cheng (william@cs.ucla.edu)
X *
X * Copyright (C) 1990, 1991, William Cheng.
X */
X#ifndef lint
Xstatic char RCSid[] =
X      "@(#)$Header: /tmp_mnt/n/kona/tangram/u/william/X11/TGIF2/RCS/xbitmap.c,v 2.0 91/03/05 14:17:51 william Exp $";
X#endif
X
X#include <stdio.h>
X#include <X11/Xlib.h>
X#include "const.h"
X#include "types.h"
X
X#include "color.e"
X#include "file.e"
X#include "mark.e"
X#include "names.e"
X#include "obj.e"
X#include "pattern.e"
X#include "raster.e"
X#include "select.e"
X#include "setup.e"
X
XGC	xbmGC = NULL;
X
Xstatic Pixmap	dummyBitmap;
X
Xstatic char	hexValue[] = "0123456789abcdef";
X
Xstatic int	numColorsToDump = 0;
Xstatic int	* pixelValue = NULL;
Xstatic int	* colorIndexToDumpIndex = NULL;
Xstatic char	* colorChar = NULL;
Xstatic char	* * colorStr = NULL;
X
Xvoid InitXBm ()
X{
X   XGCValues	values;
X
X   dummyBitmap = XCreatePixmap (mainDisplay, mainWindow, 1, 1, 1);
X
X   values.foreground = 1;
X   values.background = 0;
X   values.function = GXcopy;
X   values.fill_style = FillSolid;
X   xbmGC = XCreateGC (mainDisplay, dummyBitmap,
X         GCForeground | GCBackground | GCFunction | GCFillStyle, &values);
X}
X
Xvoid CleanUpXBm ()
X{
X   register int	i;
X
X   if (colorChar != NULL)
X   {
X      for (i = 0; i < numColorsToDump+2; i++)
X         if (colorStr[i] != NULL)
X            cfree (colorStr[i]);
X      cfree (colorStr);
X      cfree (colorChar);
X      cfree (pixelValue);
X      cfree (colorIndexToDumpIndex);
X   }
X
X   if (xbmGC != NULL) XFreeGC (mainDisplay, xbmGC);
X   XFreePixmap (mainDisplay, dummyBitmap);
X}
X
Xvoid FlipXBmHorizontal (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register int		i, left, right;
X   int			w, h, left_pixel, right_pixel;
X   Pixmap		bitmap;
X   XImage		* image;
X
X   bitmap = ObjPtr->detail.xbm->bitmap;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X   image = XGetImage (mainDisplay, bitmap, 0, 0, w, h, 1, ZPixmap);
X
X   for (i = 0; i < h; i++)
X      for (left=0, right=w-1; left < right; left++, right--)
X      {
X         left_pixel = XGetPixel (image, left, i);
X         right_pixel = XGetPixel (image, right, i);
X         if (left_pixel != right_pixel)
X         {
X            XPutPixel (image, right, i, left_pixel);
X            XPutPixel (image, left, i, right_pixel);
X         }
X      }
X   XPutImage (mainDisplay, bitmap, xbmGC, image, 0, 0, 0, 0, w, h);
X   XDestroyImage (image);
X}
X
Xvoid FlipXBmVertical (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register int		j, top, bottom;
X   int			w, h, top_pixel, bottom_pixel;
X   Pixmap		bitmap;
X   XImage		* image;
X
X   bitmap = ObjPtr->detail.xbm->bitmap;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X   image = XGetImage (mainDisplay, bitmap, 0, 0, w, h, 1, ZPixmap);
X
X   for (j = 0; j < w; j++)
X      for (top=0, bottom=h-1; top < bottom; top++, bottom--)
X      {
X         top_pixel = XGetPixel (image, j, top);
X         bottom_pixel = XGetPixel (image, j, bottom);
X         if (top_pixel != bottom_pixel)
X         {
X            XPutPixel (image, j, bottom, top_pixel);
X            XPutPixel (image, j, top, bottom_pixel);
X         }
X      }
X   XPutImage (mainDisplay, bitmap, xbmGC, image, 0, 0, 0, 0, w, h);
X   XDestroyImage (image);
X}
X
Xvoid RotateXBmClockWise (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register int		j, i;
X   int			w, h, num_bytes;
X   char			* xbm_data;
X   Pixmap		src_bitmap, dest_bitmap;
X   XImage		* src_image, * dest_image;
X
X   src_bitmap = ObjPtr->detail.xbm->bitmap;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X   src_image = XGetImage (mainDisplay, src_bitmap, 0, 0, w, h, 1, ZPixmap);
X
X   num_bytes = ((h % 8) == 0) ? (int)(h>>3) : (int)(h>>3)+1;
X
X   xbm_data = (char *) calloc (w*num_bytes, sizeof(char));
X   dest_bitmap = XCreateBitmapFromData (mainDisplay, dummyBitmap, xbm_data,
X         h, w);
X   dest_image = XGetImage (mainDisplay, dest_bitmap, 0, 0, h, w, 1, ZPixmap);
X   cfree (xbm_data);
X
X   for (i = 0; i < h; i++)
X      for (j = 0; j < w; j++)
X         if (XGetPixel (src_image, j, i) == 1)
X            XPutPixel (dest_image, h-i-1, j, 1);
X
X   XPutImage (mainDisplay, dest_bitmap, xbmGC, dest_image, 0, 0, 0, 0, h, w);
X   ObjPtr->detail.xbm->bitmap = dest_bitmap;
X
X   XDestroyImage (src_image);
X   XDestroyImage (dest_image);
X   XFreePixmap (mainDisplay, src_bitmap);
X}
X
Xvoid RotateXBmCounter (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register int		j, i;
X   int			w, h, num_bytes;
X   char			* xbm_data;
X   Pixmap		src_bitmap, dest_bitmap;
X   XImage		* src_image, * dest_image;
X
X   src_bitmap = ObjPtr->detail.xbm->bitmap;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X   src_image = XGetImage (mainDisplay, src_bitmap, 0, 0, w, h, 1, ZPixmap);
X
X   num_bytes = ((h % 8) == 0) ? (int)(h>>3) : (int)(h>>3)+1;
X
X   xbm_data = (char *) calloc (w*num_bytes, sizeof(char));
X   dest_bitmap = XCreateBitmapFromData (mainDisplay, dummyBitmap, xbm_data,
X         h, w);
X   dest_image = XGetImage (mainDisplay, dest_bitmap, 0, 0, h, w, 1, ZPixmap);
X   cfree (xbm_data);
X
X   for (i = 0; i < h; i++)
X      for (j = 0; j < w; j++)
X         if (XGetPixel (src_image, j, i) == 1)
X            XPutPixel (dest_image, i, w-j-1, 1);
X
X   XPutImage (mainDisplay, dest_bitmap, xbmGC, dest_image, 0, 0, 0, 0, h, w);
X   ObjPtr->detail.xbm->bitmap = dest_bitmap;
X
X   XDestroyImage (src_image);
X   XDestroyImage (dest_image);
X   XFreePixmap (mainDisplay, src_bitmap);
X}
X
Xstatic
Xvoid InvertXBmObject (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register int		i, j;
X   int			w, h, pixel;
X   Pixmap		bitmap;
X   XImage		* image;
X
X   bitmap = ObjPtr->detail.xbm->bitmap;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X   image = XGetImage (mainDisplay, bitmap, 0, 0, w, h, 1, ZPixmap);
X
X   for (i = 0; i < h; i++)
X      for (j = 0; j < w; j++)
X      {
X         pixel = XGetPixel (image, j, i);
X         XPutPixel (image, j, i, ((pixel==1) ? 0 : 1));
X      }
X   XPutImage (mainDisplay, bitmap, xbmGC, image, 0, 0, 0, 0, w, h);
X   XDestroyImage (image);
X}
X
Xstatic
Xint InvertObjXBitmap (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   register struct ObjRec	* obj_ptr;
X   int				changed = FALSE;
X
X   for (obj_ptr = ObjPtr; obj_ptr != NULL; obj_ptr = obj_ptr->prev)
X      switch (obj_ptr->type)
X      {
X         case OBJ_XBM: InvertXBmObject (obj_ptr); changed = TRUE; break;
X
X         case OBJ_SYM:
X         case OBJ_GROUP:
X            if (InvertObjXBitmap (obj_ptr->detail.r->last)) changed = TRUE;
X            break;
X      }
X   return (changed);
X}
X
Xvoid InvertXBitmaps ()
X{
X   register struct SelRec	* sel_ptr;
X   int				changed = FALSE;
X
X   if (topSel == NULL) return;
X
X   for (sel_ptr = botSel; sel_ptr != NULL; sel_ptr = sel_ptr->prev)
X      switch (sel_ptr->obj->type)
X      {
X         case OBJ_XBM: InvertXBmObject (sel_ptr->obj); changed = TRUE; break;
X
X         case OBJ_SYM:
X         case OBJ_GROUP:
X            if (InvertObjXBitmap (sel_ptr->obj->detail.r->last)) changed = TRUE;
X            break;
X      }
X
X   if (changed)
X   {
X      SetFileModified (TRUE);
X      HighLightReverse ();
X      RedrawAnArea (botObj, selLtX-(1<<zoomScale), selLtY-(1<<zoomScale),
X            selRbX+(1<<zoomScale), selRbY+(1<<zoomScale));
X      HighLightForward ();
X   }
X}
X
Xstatic
Xvoid DumpXBmData (FP, bitmap, W, H)
X   FILE		* FP;
X   Pixmap	bitmap;
X   int		W, H;
X{
X   register int	nibble_count, bit_count, data, i, j;
X   XImage	* image;
X
X   image = XGetImage (mainDisplay, bitmap, 0, 0, W, H, 1, ZPixmap);
X
X   nibble_count = 0;
X
X   for (i = 0; i < H; i++)
X   {
X      bit_count = 0;
X      data = 0;
X
X      for (j = 0; j < W; j++)
X      {
X         data = (XGetPixel (image, j, i) == 1) ? (data<<1) | 1 : (data<<1);
X
X         if (++bit_count == 4)
X         {
X            if (nibble_count++ == 64)
X            {
X               nibble_count = 1;
X               fprintf (FP, "\n     ");
X            }
X            fprintf (FP, "%c", hexValue[data]);
X            bit_count = 0;
X            data = 0;
X         }
X      }
X      if ((W % 4) != 0)
X      {
X         data <<= (4 - (W % 4));
X         if (nibble_count++ == 64)
X         {
X            nibble_count = 1;
X            fprintf (FP, "\n     ");
X         }
X         fprintf (FP, "%c", hexValue[data]);
X      }
X   }
X   XDestroyImage (image);
X}
X
Xstatic
Xvoid BuildXPmColors ()
X{
X   register int			i, color_index;
X   register struct ObjRec	* obj_ptr;
X
X   if (colorChar == NULL)
X   {
X      pixelValue = (int *) calloc (maxColors+2, sizeof(int));
X      colorIndexToDumpIndex = (int *) calloc (maxColors, sizeof(int));
X      colorChar = (char *) calloc (maxColors+2, sizeof(char));
X      colorStr = (char * *) calloc (maxColors+2, sizeof(char *));
X
X      for (i = 0; i < maxColors+2; i++) colorStr[i] = NULL;
X   }
X
X   for (i = 0; i < maxColors; i++) colorIndexToDumpIndex[i] = INVALID;
X
X   pixelValue[0] = myBgPixel;
X   colorChar[0] = '`';
X   colorStr[0] = (char *) calloc (strlen(myBgColorStr), sizeof(char));
X   strcpy (colorStr[0], myBgColorStr);
X   numColorsToDump = 1;
X
X   for (obj_ptr = botObj; obj_ptr != NULL; obj_ptr = obj_ptr->prev)
X   {
X      color_index = obj_ptr->color;
X      if (colorIndexToDumpIndex[color_index] == INVALID)
X      {
X         if (colorStr[numColorsToDump] != NULL)
X            cfree (colorStr[numColorsToDump]);
X
X         pixelValue[numColorsToDump] = colorPixels[color_index];
X         colorIndexToDumpIndex[color_index] = numColorsToDump;
X         colorChar[numColorsToDump] = (char)(((int)('a'))+numColorsToDump-1);
X         colorStr[numColorsToDump] = (char *) calloc
X               (strlen(colorMenuItems[color_index]), sizeof(char));
X         strcpy (colorStr[numColorsToDump], colorMenuItems[color_index]);
X         numColorsToDump++;
X      }
X   }
X}
X
Xstatic
Xvoid DumpXPmColors (FP)
X   FILE	* FP;
X{
X   register int	i;
X
X   for (i = 0; i < numColorsToDump-1; i++)
X      fprintf (FP, "   \"%c\", \"%s\",\n", colorChar[i], colorStr[i]);
X   fprintf (FP, "   \"%c\", \"%s\"\n};\n", colorChar[i], colorStr[i]);
X}
X
Xvoid DumpXBitmapFile ()
X{
X   register int	j, bit_count, index, data;
X   int		i, len, ltx, lty, w, h, byte_count;
X   char		xbm_file_name[MAXPATHLENGTH], msg[MAXSTRING];
X   char		name[MAXPATHLENGTH];
X   FILE		* fp;
X   Pixmap	pixmap;
X   XImage	* image;
X
X   strcpy (name, curFileName);
X   len = strlen (name);
X   for (j = len-1; name[j] != '.'; j--) ;
X   name[j] = '\0';
X
X   sprintf (xbm_file_name, "%s/%s", curDir, curFileName);
X   len = strlen (xbm_file_name);
X   for (j = len-1; xbm_file_name[j] != '.'; j--) ;
X   if (colorDump)
X      sprintf (&xbm_file_name[j], ".%s", XPM_FILE_EXT);
X   else
X      sprintf (&xbm_file_name[j], ".%s", XBM_FILE_EXT);
X
X   if ((fp = fopen (xbm_file_name, "w")) == NULL)
X   {
X      sprintf (msg, "Can not open '%s', print aborted.", xbm_file_name);
X      Msg (msg);
X      return;
X   }
X   sprintf (msg, "Printing into '%s'.", xbm_file_name);
X   Msg (msg);
X
X   if ((pixmap = DrawAllOnPixmap (&ltx, &lty, &w, &h)) == None) return;
X
X   if (colorDump)
X   {
X      BuildXPmColors ();
X      fprintf (fp, "#define %s_format 1\n", name);
X      fprintf (fp, "#define %s_width %1d\n", name, w);
X      fprintf (fp, "#define %s_height %1d\n", name, h);
X      fprintf (fp, "#define %s_ncolors %1d\n", name, numColorsToDump);
X      fprintf (fp, "#define %s_chars_per_pixel 1\n", name);
X      fprintf (fp, "static char *%s_colors[] = {\n", name);
X      DumpXPmColors (fp);
X      fprintf (fp, "static char *%s_pixels[] = {\n", name);
X   }
X   else
X   {
X      fprintf (fp, "#define %s_width %1d\n", name, w);
X      fprintf (fp, "#define %s_height %1d\n", name, h);
X      fprintf (fp, "#define %s_x_hot 0\n", name);
X      fprintf (fp, "#define %s_y_hot 0\n", name);
X      fprintf (fp, "static char %s_bits[] = {\n   ", name);
X   }
X   image = XGetImage (mainDisplay, pixmap, 0, 0, w, h, AllPlanes, ZPixmap);
X
X   if (colorDump)
X   {
X      for (i = 0; i < h; i++)
X      {
X         fprintf (fp, "\"");
X         for (j = 0; j < w; j++)
X         {
X            data = XGetPixel (image,j,i);
X            if (data == myBgPixel)
X               fprintf (fp, "`");
X            else
X            {
X               for (index = 0; index < numColorsToDump; index++)
X                  if (pixelValue[index] == data)
X                     break;
X               if (index == numColorsToDump)
X               {
X                  sprintf (msg, "Unrecognized pixel value %1d!  %s %s.",
X                        data, "Print aborted!");
X                  Msg (msg);
X
X                  fclose (fp);
X                  XDestroyImage (image);
X                  XFreePixmap (mainDisplay, pixmap);
X                  return;
X               }
X               fprintf (fp, "%c", colorChar[index]);
X            }
X         }
X         if (i == h-1)
X            fprintf (fp, "\"\n};\n");
X         else
X            fprintf (fp, "\",\n");
X      }
X   }
X   else
X   {
X      byte_count = 0;
X      for (i = 0; i < h; i++)
X      {
X         bit_count = 0;
X         data = 0;
X
X         for (j = 0; j < w; j++)
X         {
X            if (XGetPixel (image,j,i) != myBgPixel) data |= (1<<bit_count);
X
X            if (++bit_count == 8)
X            {
X               if (byte_count++ == 12)
X               {
X                  byte_count = 1;
X                  fprintf (fp, "\n   ");
X               }
X               fprintf (fp, "0x%c", hexValue[(data>>4) & 0xf]);
X
X               if (i == h-1 && j == w-1)
X                  fprintf (fp, "%c};\n", hexValue[data & 0xf]);
X               else
X                  fprintf (fp, "%c, ", hexValue[data & 0xf]);
X
X               bit_count = 0;
X               data = 0;
X            }
X         }
X         if ((w % 8) != 0)
X         {
X            if (byte_count++ == 12)
X            {
X               byte_count = 1;
X               fprintf (fp, "\n   ");
X            }
X            fprintf (fp, "0x%c", hexValue[(data>>4) & 0xf]);
X            if (i == h-1)
X               fprintf (fp, "%c};\n", hexValue[data & 0xf]);
X            else
X               fprintf (fp, "%c, ", hexValue[data & 0xf]);
X         }
X      }
X   }
X   fclose (fp);
X
X   XDestroyImage (image);
X   XFreePixmap (mainDisplay, pixmap);
X}
X
Xvoid DumpXBmObj (FP, ObjPtr, PRTGIF)
X   FILE			* FP;
X   struct ObjRec	* ObjPtr;
X   int			PRTGIF;
X{
X   register int	i, j;
X   int		ltx, lty, rbx, rby, w, h, color_index;
X   int		num_nibbles, nibble_count, fill;
X   Pixmap	bitmap;
X   char		* xbm_data;
X
X   ltx = ObjPtr->obbox.ltx;
X   lty = ObjPtr->obbox.lty;
X   rbx = ObjPtr->obbox.rbx;
X   rby = ObjPtr->obbox.rby;
X
X   fill = ObjPtr->detail.xbm->fill;
X
X   w = rbx - ltx;
X   h = rby - lty;
X
X   fprintf (FP, "gsave\n");
X
X   color_index = ObjPtr->color;
X   if (colorDump)
X   {
X      fprintf (FP, "   %.3f %.3f %.3f setrgbcolor\n",
X            ((float)tgifColors[color_index].red/maxRGB),
X            ((float)tgifColors[color_index].green/maxRGB),
X            ((float)tgifColors[color_index].blue/maxRGB));
X   }
X   else
X      fprintf (FP, "   0 setgray\n");
X
X   switch (fill)
X   {
X      case NONEPAT: break;
X      case SOLIDPAT:
X         fprintf (FP, "   newpath\n");
X         fprintf (FP, "      %1d %1d moveto\n", ltx, lty);
X         fprintf (FP, "      %1d %1d lineto\n", rbx, lty);
X         fprintf (FP, "      %1d %1d lineto\n", rbx, rby);
X         fprintf (FP, "      %1d %1d lineto\n", ltx, rby);
X         fprintf (FP, "   closepath fill\n\n");
X         break;
X      case BACKPAT:
X         fprintf (FP, "   newpath\n");
X         fprintf (FP, "      %1d %1d moveto\n", ltx, lty);
X         fprintf (FP, "      %1d %1d lineto\n", rbx, lty);
X         fprintf (FP, "      %1d %1d lineto\n", rbx, rby);
X         fprintf (FP, "      %1d %1d lineto\n", ltx, rby);
X         fprintf (FP, "   closepath 1 setgray fill\n\n");
X         if (colorDump)
X            fprintf (FP, "   %.3f %.3f %.3f setrgbcolor\n",
X                  ((float)tgifColors[color_index].red/maxRGB),
X                  ((float)tgifColors[color_index].green/maxRGB),
X                  ((float)tgifColors[color_index].blue/maxRGB));
X         else
X            fprintf (FP, "   0 setgray\n");
X         break;
X      default:
X         /* patterned */
X         fprintf (FP, "   gsave\n");
X         if (!colorDump)
X            fprintf (FP, "   pat%1d 8 1 0 72 300 32 div div setpattern\n",fill);
X         fprintf (FP, "      newpath\n");
X         fprintf (FP, "         %1d %1d moveto\n", ltx, lty);
X         fprintf (FP, "         %1d %1d lineto\n", rbx, lty);
X         fprintf (FP, "         %1d %1d lineto\n", rbx, rby);
X         fprintf (FP, "         %1d %1d lineto\n", ltx, rby);
X         if (colorDump)
X         {
X            fprintf (FP, "      closepath eoclip\n");
X            DumpPatFill (FP, fill, 8, ObjPtr->bbox, "      ");
X         }
X         else
X            fprintf (FP, "      closepath fill\n");
X         fprintf (FP, "   grestore\n\n");
X         break;
X   }
X
X   fprintf (FP, "   %1d %1d translate\n", ltx, lty);
X   fprintf (FP, "   %1d %1d true [1 0 0 1 0 0]\n   {<", w, h);
X   if (PRTGIF)
X   {
X      xbm_data = ObjPtr->detail.xbm->data;
X
X      num_nibbles = ((w % 4) == 0) ? (int)(w>>2) : (int)(w>>2)+1;
X      nibble_count = 0;
X
X      for (i = 0; i < h; i++)
X      {
X         for (j = 0; j < num_nibbles; j++)
X         {
X            if (nibble_count++ == 64)
X            {
X               nibble_count = 1;
X               fprintf (FP, "\n     ");
X            }
X            fprintf (FP, "%c", xbm_data[i*num_nibbles+j]);
X         }
X      }
X   }
X   else
X   {
X      bitmap = ObjPtr->detail.xbm->bitmap;
X      DumpXBmData (FP, bitmap, w, h);
X   }
X   fprintf (FP, ">}\n   imagemask\n");
X   fprintf (FP, "grestore\n\n");
X
X}
X
Xvoid DrawXBmObj (win, XOff, YOff, ObjPtr)
X   Window		win;
X   struct ObjRec	* ObjPtr;
X{
X   register int		i, j;
X   int			r, c, ltx, lty, rbx, rby, w, h, scr_w, scr_h;
X   int			real_x_off, real_y_off, scale, num_cols, num_rows;
X   int			fg_pixel, row_start, row_end, col_start, col_end;
X   int			count, threshold, fill;
X   struct XBmRec	* xbm_ptr = ObjPtr->detail.xbm;
X   XGCValues		values;
X   XImage		* image;
X
X   fill = xbm_ptr->fill;
X
X   w = ObjPtr->obbox.rbx - ObjPtr->obbox.ltx;
X   h = ObjPtr->obbox.rby - ObjPtr->obbox.lty;
X
X   real_x_off = (XOff >> zoomScale) << zoomScale;
X   real_y_off = (YOff >> zoomScale) << zoomScale;
X   ltx = (ObjPtr->obbox.ltx - real_x_off) >> zoomScale;
X   lty = (ObjPtr->obbox.lty - real_y_off) >> zoomScale;
X   rbx = (ObjPtr->obbox.rbx - real_x_off) >> zoomScale;
X   rby = (ObjPtr->obbox.rby - real_y_off) >> zoomScale;
X   scr_w = rbx - ltx;
X   scr_h = rby - lty;
X
X   if (fill != 0)
X   {
X      values.foreground = (fill == 2) ? myBgPixel : colorPixels[ObjPtr->color];
X      values.function = GXcopy;
X      values.fill_style = FillOpaqueStippled;
X      values.stipple = patPixmap[fill];
X      XChangeGC (mainDisplay, drawGC,
X            GCForeground | GCFunction | GCFillStyle | GCStipple, &values);
X      XFillRectangle (mainDisplay, win, drawGC, ltx, lty, scr_w, scr_h);
X   }
X
X   if (zoomScale == 0)
X   {
X      values.foreground = colorPixels[ObjPtr->color];
X      values.function = GXcopy;
X      values.fill_style = FillStippled;
X      values.stipple = xbm_ptr->bitmap;
X      values.ts_x_origin = ltx;
X      values.ts_y_origin = lty;
X      XChangeGC (mainDisplay, drawGC,
X            GCForeground | GCFunction | GCFillStyle | GCStipple |
X            GCTileStipXOrigin | GCTileStipYOrigin, &values);
X      XFillRectangle (mainDisplay, win, drawGC, ltx, lty, scr_w, scr_h);
X      XSetTSOrigin (mainDisplay, drawGC, 0, 0);
X   }
X   else
X   {
X      values.function = GXcopy;
X      values.fill_style = FillSolid;
X      XChangeGC (mainDisplay, drawGC, GCFunction | GCFillStyle, &values);
X
X      fg_pixel = colorPixels[ObjPtr->color];
X
X      scale = 1<<zoomScale;
X      threshold = scale*scale/2;
X      num_cols = ((w % scale) == 0) ? (w/scale) : (w/scale+1);
X      num_rows = ((h % scale) == 0) ? (h/scale) : (h/scale+1);
X
X      image = XGetImage (mainDisplay, xbm_ptr->bitmap, 0, 0, w, h, 1, ZPixmap);
X
X      for (r = 0; r < num_rows-1; r++)
X      {
X         row_start = scale*r;
X         row_end = min(scale*(r+1), w);
X         for (c = 0; c < num_cols-1; c++)
X         {
X            col_start = scale*c;
X            col_end = min(scale*(c+1), w);
X
X            count = 0;
X            for (i = row_start; count < threshold && i < row_end; i++)
X               for (j = col_start; count < threshold && j < col_end; j++)
X                  if (XGetPixel (image, j, i) == 1)
X                     count++;
X            if (count >= threshold)
X            {
X               XSetForeground (mainDisplay, drawGC, fg_pixel);
X#ifdef sun
X               XDrawPoint (mainDisplay, win, drawGC, c+ltx, r+lty);
X#else
X#ifdef ultrix
X               XDrawPoint (mainDisplay, win, drawGC, c+ltx, r+lty);
X#else
X               XDrawLine (mainDisplay, win, drawGC, c+ltx, r+lty, c+ltx, r+lty);
X#endif
X#endif
X            }
X            else
X            {
X               XSetForeground (mainDisplay, drawGC, myBgPixel);
X#ifdef sun
X               XDrawPoint (mainDisplay, win, drawGC, c+ltx, r+lty);
X#else
X#ifdef ultrix
X               XDrawPoint (mainDisplay, win, drawGC, c+ltx, r+lty);
X#else
X               XDrawLine (mainDisplay, win, drawGC, c+ltx, r+lty, c+ltx, r+lty);
X#endif
X#endif
X            }
X         }
X      }
X      XDestroyImage (image);
X   }
X}
X
Xvoid CreateXBmObj (W, H, bitmap)
X   int		W, H;
X   Pixmap	bitmap;
X{
X   struct XBmRec	* xbm_ptr;
X   struct ObjRec	* obj_ptr;
X
X   xbm_ptr = (struct XBmRec *) calloc (1, sizeof(struct XBmRec));
X   xbm_ptr->bitmap = bitmap;
X   xbm_ptr->data = NULL;
X   xbm_ptr->fill = objFill;
X
X   obj_ptr = (struct ObjRec *) calloc (1, sizeof(struct ObjRec));
X
X   obj_ptr->bbox.ltx = obj_ptr->obbox.ltx = obj_ptr->x = drawOrigX;
X   obj_ptr->bbox.lty = obj_ptr->obbox.lty = obj_ptr->y = drawOrigY;
X   obj_ptr->bbox.rbx = obj_ptr->obbox.rbx = W + drawOrigX;
X   obj_ptr->bbox.rby = obj_ptr->obbox.rby = H + drawOrigY;
X   obj_ptr->type = OBJ_XBM;
X   obj_ptr->color = colorIndex;
X   obj_ptr->id = objId++;
X   obj_ptr->dirty = FALSE;
X   obj_ptr->detail.xbm = xbm_ptr;
X   obj_ptr->fattr = obj_ptr->lattr = NULL;
X   AddObj (NULL, topObj, obj_ptr);
X}
X 
Xvoid SaveXBmObj (FP, ObjPtr)
X   FILE			* FP;
X   struct ObjRec	* ObjPtr;
X{
X   int	ltx, lty, rbx, rby, w, h;
X
X   ltx = ObjPtr->obbox.ltx; lty = ObjPtr->obbox.lty;
X   rbx = ObjPtr->obbox.rbx; rby = ObjPtr->obbox.rby;
X   w = rbx - ltx;
X   h = rby - lty;
X   fprintf (FP, "xbm('%s',", colorMenuItems[ObjPtr->color]);
X   fprintf (FP, "%1d,%1d,%1d,%1d,%1d,%1d,\n    \"",
X         ltx, lty, rbx, rby, ObjPtr->detail.xbm->fill, ObjPtr->id);
X   DumpXBmData (FP, ObjPtr->detail.xbm->bitmap, w, h);
X   fprintf (FP, "\",");
X
X   SaveAttrs (FP, ObjPtr->lattr);
X   fprintf (FP, ")");
X}
X
Xvoid ReadXBmObj (FP, Inbuf, ObjPtr, PRTGIF)
X   FILE			* FP;
X   char			* Inbuf;
X   struct ObjRec	* * ObjPtr;
X   int			PRTGIF;
X{
X   struct XBmRec	* xbm_ptr;
X   char			color_str[20], * s, inbuf[MAXSTRING], * c_ptr;
X   int			ltx, lty, rbx, rby, w, h, i, j, k, data=0, color_index;
X   int			nibble_count, bit_count, num_nibbles, num_bytes, fill;
X   Pixmap		bitmap;
X   char			* xbm_data;
X   XImage		* image;
X
X   *ObjPtr = (struct ObjRec *) calloc (1, sizeof(struct ObjRec));
X   s = FindChar ('(', Inbuf);
X   s = ParseStr (s, ',', color_str);
X   xbm_ptr = (struct XBmRec *) calloc (1, sizeof(struct XBmRec));
X
X   if (fileVersion > 8)
X   {
X      sscanf (s, "%d , %d , %d , %d , %d , %d",
X            &ltx, &lty, &rbx, &rby, &fill, &((*ObjPtr)->id));
X      if ((*ObjPtr)->id >= objId) objId = (*ObjPtr)->id + 1;
X   }
X
X   color_index = FindColorIndex (color_str);
X
X   (*ObjPtr)->color = color_index;
X   (*ObjPtr)->dirty = FALSE;
X   (*ObjPtr)->type = OBJ_XBM;
X   (*ObjPtr)->obbox.ltx = (*ObjPtr)->bbox.ltx = (*ObjPtr)->x = ltx;
X   (*ObjPtr)->obbox.lty = (*ObjPtr)->bbox.lty = (*ObjPtr)->y = lty;
X   (*ObjPtr)->obbox.rbx = (*ObjPtr)->bbox.rbx = rbx;
X   (*ObjPtr)->obbox.rby = (*ObjPtr)->bbox.rby = rby;
X   (*ObjPtr)->detail.xbm = xbm_ptr;
X
X   xbm_ptr->fill = fill;
X
X   w = rbx - ltx;
X   h = rby - lty;
X   num_nibbles = ((w % 4) == 0) ? (int)(w>>2) : (int)(w>>2)+1;
X   num_bytes = ((w % 8) == 0) ? (int)(w>>3) : (int)(w>>3)+1;
X
X   fgets (inbuf, MAXSTRING, FP);
X   c_ptr = &inbuf[5];
X   nibble_count = 0;
X
X   if (PRTGIF)
X   {
X      xbm_data = (char *) calloc (h*num_nibbles, sizeof(char));
X      for (i = 0; i < h; i++)
X      {
X         for (j = 0; j < num_nibbles; j++)
X         {
X            if (nibble_count++ == 64)
X            {
X               fgets (inbuf, MAXSTRING, FP);
X               c_ptr = &inbuf[5];
X               nibble_count = 1;
X            }
X            xbm_data[i*num_nibbles+j] = *c_ptr++;
X         }
X      }
X      xbm_ptr->data = xbm_data;
X   }
X   else
X   {
X      xbm_data = (char *) calloc (h*num_bytes, sizeof(char));
X      bitmap = XCreateBitmapFromData (mainDisplay, dummyBitmap, xbm_data, w, h);
X      image = XGetImage (mainDisplay, bitmap, 0, 0, w, h, 1, ZPixmap);
X      cfree (xbm_data);
X
X      for (i = 0; i < h; i++)
X      {
X         bit_count = 0;
X         for (j = 0; j < num_nibbles; j++)
X         {
X            if (nibble_count++ == 64)
X            {
X               fgets (inbuf, MAXSTRING, FP);
X               c_ptr = &inbuf[5];
X               nibble_count = 1;
X            }
X
X            if (*c_ptr >= '0' && *c_ptr <= '9')
X               data = (int)(*c_ptr++) - (int)('0');
X            else if (*c_ptr >= 'a' && *c_ptr <= 'f')
X               data = (int)(*c_ptr++) - (int)('a') + 10;
X
X            for (k = 0; k < 4; k++)
X            {
X               if (bit_count++ == w) break;
X
X               if (data & (1<<(3-k)))
X                  XPutPixel (image, j*4+k, i, 1);
X            }
X         }
X      }
X      XPutImage (mainDisplay, bitmap, xbmGC, image, 0, 0, 0, 0, w, h);
X      xbm_ptr->bitmap = bitmap;
X
X      XDestroyImage (image);
X   }
X}
X
Xvoid FreeXBmObj (ObjPtr)
X   struct ObjRec	* ObjPtr;
X{
X   XFreePixmap (mainDisplay, ObjPtr->detail.xbm->bitmap);
X   if (ObjPtr->detail.xbm->data != NULL) cfree (ObjPtr->detail.xbm->data);
X   cfree (ObjPtr->detail.xbm);
X   cfree (ObjPtr);
X}
END_OF_FILE
if test 25875 -ne `wc -c <'xbitmap.c'`; then
    echo shar: \"'xbitmap.c'\" unpacked with wrong size!
fi
# end of 'xbitmap.c'
fi
echo shar: End of archive 17 \(of 23\).
cp /dev/null ark17isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 23 archives.
    rm -f ark[1-9]isdone ark[1-9][0-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
---------------------------------> cut here <---------------------------------
-- 
Bill Cheng // UCLA Computer Science Department // (213) 206-7135
3277 Boelter Hall // Los Angeles, California 90024 // USA
william@CS.UCLA.EDU      ...!{uunet|ucbvax}!cs.ucla.edu!william

--
Dan Heller
------------------------------------------------
O'Reilly && Associates		 Z-Code Software
Senior Writer			       President
argv@ora.com			argv@zipcode.com
------------------------------------------------
General Email: argv@sun.com
Comp-sources-x stuff: comp-sources.x@uunet.uu.net