home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / fbm / part03 / flread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-08  |  4.2 KB  |  131 lines

  1. /*****************************************************************
  2.  * flread.c: FBM Library 0.9 (Beta test) 07-Mar-89  Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989 by Michael Mauldin.  Permission is granted to
  5.  * use this file in whole or in part provided that you do not sell it
  6.  * for profit and that this copyright notice is retained unchanged.
  7.  *
  8.  * flread.c: 
  9.  *
  10.  * CONTENTS
  11.  *    read_bitmap (image, rfname)
  12.  *    write_bitmap (image, wfile, type)
  13.  *
  14.  * EDITLOG
  15.  *    LastEditDate = Tue Mar  7 19:57:27 1989 - Michael Mauldin
  16.  *    LastFileName = /usr2/mlm/src/misc/fbm/flread.c
  17.  *
  18.  * HISTORY
  19.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  20.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  21.  *
  22.  * 28-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  23.  *    Created.
  24.  *****************************************************************/
  25.  
  26. # include <stdio.h>
  27. # include "fbm.h"
  28.  
  29. # define BM_MAGC    '!'    /* bm format (!!) */
  30. # define FBM_MAGC    '%'    /* fbm format (%bitmap) */
  31. # define SUN_MAGC    0x59    /* Sun rasterfile (0x59a66a95) */
  32. # define PBM_MAGC    'P'    /* pbm file (P1) */
  33. # define ATK_MAGC    '\\'    /* ATK file (\begindata{raster,length}) */
  34. # define MP_MAGC    '\0'    /* MacPaint (titlength in short) */
  35. # define X11_MAGC    '#'    /* X bitmaps (#define) */
  36. # define PCX_MAGC    0xa    /* PCX, PC Paintbrush files */
  37. # define IFF_MAGC    'F'    /* Amiga IFF format ("FORM") */
  38. # define GIF_MAGC    'G'    /* GIF format (CompuServe) */
  39.  
  40. # define COMPRESSED_CHAR 0x1f    /* Compressed file (0x1f9d) */
  41.  
  42. #ifndef lint
  43. static char *fbmid =
  44.     "$FBM flread.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  45. #endif
  46.  
  47. read_bitmap (image, fname)
  48. FBM *image;
  49. char *fname;
  50. { int peekch, result = 0, do_pclose = 0;
  51.   FILE *rfile;
  52.   char cmd[256], magic[32], mlen = 0;
  53.  
  54.   /* Open the file if name given, otherwise assume stdin */
  55.   if (fname == NULL || *fname == '\0' || !strcmp (fname, "-"))
  56.   { rfile = stdin; }
  57.   else
  58.   { if ((rfile = fopen (fname, "r")) == NULL)
  59.     { perror (fname);  return (0); }
  60.   }
  61.  
  62.   /* Guess image type by reading the first character */
  63.   peekch = fgetc (rfile);
  64.  
  65.   /* If the image is compressed, uncompress it while reading */  
  66.   if (peekch == COMPRESSED_CHAR)
  67.   { if (rfile == stdin)
  68.     { if (lseek (fileno (rfile), 0L, 0) < 0)  perror ("lseek");
  69.       rfile = popen ("uncompress", "r");
  70.     }
  71.     else
  72.     { fclose (rfile);
  73.       sprintf (cmd, "uncompress < %s", fname);
  74.       rfile = popen (cmd, "r");
  75.     }
  76.     do_pclose++;
  77.  
  78.     peekch = fgetc (rfile);
  79.   }
  80.   
  81.   magic[0] = peekch;
  82.   mlen = 1;
  83.  
  84.   /* Dispatch on the magic character */
  85.   switch (peekch)
  86.   { case BM_MAGC:    result = read_face (image, rfile, magic, mlen); break;
  87.     case FBM_MAGC:    result = read_fbm (image, rfile, magic, mlen); break;
  88.     case SUN_MAGC:    result = read_sun (image, rfile, magic, mlen); break;
  89.     case PBM_MAGC:    result = read_pbm (image, rfile, magic, mlen); break;
  90.     case PCX_MAGC:    result = read_pcx (image, rfile, magic, mlen); break;
  91.     case GIF_MAGC:    result = read_gif (image, rfile, magic, mlen); break;
  92.     case IFF_MAGC:    result = read_iff (image, rfile, magic, mlen); break;
  93.     case ATK_MAGC:    fprintf (stderr, "Can't read Andrew rasters\n"); break;
  94.     case MP_MAGC:    fprintf (stderr, "Can't read MacPaint files\n"); break;
  95.     case X11_MAGC:    fprintf (stderr, "Can't read X bitmaps\n"); break;
  96.     case EOF:        fprintf (stderr, "Empty file"); break;
  97.     default:        fprintf (stderr, "Unknown magic char %03o\n", peekch);
  98.   }
  99.   
  100.   if (do_pclose)    pclose (rfile);
  101.   else            fclose (rfile);
  102.   
  103.   return (result);
  104. }
  105.  
  106. /****************************************************************
  107.  * write_bitmap: Write a specified format
  108.  ****************************************************************/
  109.  
  110. write_bitmap (image, wfile, type)
  111. FBM *image;
  112. FILE *wfile;
  113. int type;
  114. {
  115.   switch (type)
  116.   {
  117.     case FMT_PBM:    if (! write_pbm (image, wfile)) return (0); break;
  118.     case FMT_FBM:    if (! write_fbm (image, wfile)) return (0); break;
  119.     case FMT_FACE:    if (! write_face (image, wfile)) return (0); break;
  120.     case FMT_SUN:    if (! write_sun (image, wfile)) return (0); break;
  121.     case FMT_GIF:    if (! write_gif (image, wfile)) return (0); break;
  122.     case FMT_IFF:    if (! write_iff (image, wfile)) return (0); break;
  123.     default:        fprintf (stderr,  
  124.                  "write_bitmap: unknown format type %d\n",
  125.                  type);
  126.                 return (0);
  127.   }
  128.   
  129.   return (1);
  130. }
  131.