home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fbm / src / flread.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  5KB  |  142 lines

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