home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / bitmap / bmtoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-18  |  3.9 KB  |  172 lines

  1. /*
  2.  * bmtoa - bitmap to ascii filter
  3.  *
  4.  * $XConsortium: bmtoa.c,v 1.2 91/02/18 15:05:44 dave Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Jim Fulton, MIT X Consortium
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <X11/Xlib.h>
  23. #include <X11/Xutil.h>
  24.  
  25. #include <X11/Xmu/Drawing.h>
  26.  
  27. extern char *malloc();
  28.  
  29. char *ProgramName;
  30.  
  31.  
  32. static void usage ()
  33. {
  34.     fprintf (stderr, "usage:  %s [-options ...] [filename]\n\n",
  35.          ProgramName);
  36.     fprintf (stderr, 
  37.     "where options include:\n");
  38.     fprintf (stderr,
  39.     "    -chars cc        chars to use for 0 and 1 bits, respectively\n");
  40.     fprintf (stderr, "\n");
  41.     exit (1);
  42. }
  43.  
  44. static char *copy_stdin ()
  45. {
  46.     static char tmpfilename[] = "/tmp/bmtoa.XXXXXX";
  47.     char buf[BUFSIZ];
  48.     FILE *fp;
  49.     int nread, nwritten;
  50.  
  51.     if (mktemp (tmpfilename) == NULL) {
  52.     fprintf (stderr,
  53.          "%s:  unable to genererate temporary file name for stdin.\n",
  54.          ProgramName);
  55.     exit (1);
  56.     }
  57.     fp = fopen (tmpfilename, "w");
  58.     while (1) {
  59.     buf[0] = '\0';
  60.     nread = fread (buf, 1, sizeof buf, stdin);
  61.     if (nread <= 0) break;
  62.     nwritten = fwrite (buf, 1, nread, fp);
  63.     if (nwritten != nread) {
  64.         fprintf (stderr,
  65.              "%s:  error copying stdin to file (%d of %d chars)\n",
  66.              ProgramName, nwritten, nread);
  67.         (void) fclose (fp);
  68.         (void) unlink (tmpfilename);
  69.         exit (1);
  70.     }
  71.     }
  72.     (void) fclose (fp);
  73.     return tmpfilename;
  74. }
  75.  
  76. main (argc, argv) 
  77.     int argc;
  78.     char **argv;
  79. {
  80.     char *filename = NULL;
  81.     int isstdin = 0;
  82.     char *chars = "-#";
  83.     int i;
  84.     unsigned int width, height;
  85.     unsigned char *data;
  86.     int x_hot, y_hot;
  87.     int status;
  88.  
  89.     ProgramName = argv[0];
  90.  
  91.     for (i = 1; i < argc; i++) {
  92.     char *arg = argv[i];
  93.  
  94.     if (arg[0] == '-') {
  95.         switch (arg[1]) {
  96.           case '\0':
  97.         filename = NULL;
  98.         continue;
  99.           case 'c':
  100.         if (++i >= argc) usage ();
  101.         chars = argv[i];
  102.         continue;
  103.           default:
  104.         usage ();
  105.         }
  106.     } else {
  107.         filename = arg;
  108.     }
  109.     }
  110.  
  111.     if (strlen (chars) != 2) {
  112.     fprintf (stderr,
  113.      "%s:  bad character list \"%s\", must have exactly 2 characters\n",
  114.          ProgramName, chars);
  115.     exit (1);
  116.     }
  117.  
  118.     if (!filename) {
  119.     filename = copy_stdin ();
  120.     isstdin = 1;
  121.     }
  122.  
  123.     status = XmuReadBitmapDataFromFile (filename, &width, &height, &data,
  124.                     &x_hot, &y_hot);
  125.     if (isstdin) (void) unlink (filename);  /* don't need it anymore */
  126.     if (status != BitmapSuccess) {
  127.     fprintf (stderr, "%s:  unable to read bitmap from file \"%s\"\n",
  128.          ProgramName, isstdin ? "(stdin)" : filename);
  129.     exit (1);
  130.     }
  131.  
  132.     print_scanline (width, height, data, chars);
  133.     exit (0);
  134. }
  135.  
  136. print_scanline (width, height, data, chars)
  137.     unsigned int width, height;
  138.     unsigned char *data;
  139.     char *chars;
  140. {
  141.     char *scanline = (char *) malloc (width + 1);
  142.     unsigned char *dp = data;
  143.     int row, column;
  144.     static unsigned char masktable[] = {
  145.     0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
  146.     int padded = ((width & 7) != 0);
  147.  
  148.     if (!scanline) {
  149.     fprintf (stderr, "%s:  unable to allocate %d bytes for scanline\n",
  150.          ProgramName, width + 1);
  151.     exit (1);
  152.     }
  153.  
  154.     for (row = 0; row < height; row++) {
  155.     for (column = 0; column < width; column++) {
  156.         int i = (column & 7);
  157.  
  158.         if (*dp & masktable[i]) {
  159.         putchar (chars[1]);
  160.         } else {
  161.         putchar (chars[0]);
  162.         }
  163.  
  164.         if (i == 7) dp++;
  165.     }
  166.     putchar ('\n');
  167.     if (padded) dp++;
  168.     }
  169.     return;
  170. }
  171.  
  172.