home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / flread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  5.1 KB  |  150 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 TIF_MAGC    'M'    /* TIFF format (MM) */
  45. # define RLE_MAGC    0x52    /* Utah RLE file (0x52 0xcc) */
  46. # define JPEG_MAGC    0xff    /* Jpeg JFIF format ffd8ffe0 00104a46 4946 */
  47.  
  48. # define COMPRESSED_CHAR 0x1f    /* Compressed file (0x1f9d) */
  49.  
  50. #ifndef lint
  51. static char *fbmid =
  52. "$FBM flread.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  53. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  54. #endif
  55.  
  56. read_bitmap (image, fname)
  57. FBM *image;
  58. char *fname;
  59. { int peekch, result = 0, do_pclose = 0;
  60.   FILE *rfile;
  61.   char cmd[256], magic[32], mlen = 0;
  62.  
  63.   /* Open the file if name given, otherwise assume stdin */
  64.   if (fname == NULL || *fname == '\0' || !strcmp (fname, "-"))
  65.   { rfile = stdin; }
  66.   else
  67.   { if ((rfile = fopen (fname, "r")) == NULL)
  68.     { perror (fname);  return (0); }
  69.   }
  70.  
  71.   /* Guess image type by reading the first character */
  72.   peekch = fgetc (rfile);
  73.  
  74.   /* If the image is compressed, uncompress it while reading */  
  75.   if (peekch == COMPRESSED_CHAR)
  76.   { if (rfile == stdin)
  77.     { if (lseek (fileno (rfile), 0L, 0) < 0)  perror ("lseek");
  78.       rfile = popen ("uncompress", "r");
  79.     }
  80.     else
  81.     { fclose (rfile);
  82.       sprintf (cmd, "uncompress < %s", fname);
  83.       rfile = popen (cmd, "r");
  84.     }
  85.     do_pclose++;
  86.  
  87.     peekch = fgetc (rfile);
  88.   }
  89.   
  90.   magic[0] = peekch;
  91.   mlen = 1;
  92.  
  93.   /* Dispatch on the magic character */
  94.   switch (peekch)
  95.   { case BM_MAGC:    result = read_face (image, rfile, magic, mlen); break;
  96.     case FBM_MAGC:    result = read_fbm (image, rfile, magic, mlen); break;
  97.     case SUN_MAGC:    result = read_sun (image, rfile, magic, mlen); break;
  98.     case PBM_MAGC:    result = read_pbm (image, rfile, magic, mlen); break;
  99.     case PCX_MAGC:    result = read_pcx (image, rfile, magic, mlen); break;
  100.     case GIF_MAGC:    result = read_gif (image, rfile, magic, mlen); break;
  101.     case IFF_MAGC:    result = read_iff (image, rfile, magic, mlen); break;
  102.     case ATK_MAGC:    fprintf (stderr, "Can't read Andrew rasters\n"); break;
  103.     case MP_MAGC:    fprintf (stderr, "Can't read MacPaint files\n"); break;
  104.     case X11_MAGC:    fprintf (stderr, "Can't read X bitmaps\n"); break;
  105.     case RLE_MAGC:    result = read_rle (image, rfile, magic, mlen); break;
  106.     case TIF_MAGC:    fprintf (stderr, "Can't read TIFF directly, use tiff2fbm first\n"); break;
  107.     case JPEG_MAGC:    ungetc (peekch, rfile); 
  108.             result = read_jpeg (image, rfile, magic, mlen);
  109.             break;
  110.     case EOF:        fprintf (stderr, "Empty file"); break;
  111.     default:        fprintf (stderr, "Unknown magic char %03o\n", peekch);
  112.   }
  113.   
  114.   if (do_pclose)    pclose (rfile);
  115.   else            fclose (rfile);
  116.   
  117.   return (result);
  118. }
  119.  
  120. /****************************************************************
  121.  * write_bitmap: Write a specified format
  122.  ****************************************************************/
  123.  
  124. int quality = 75;
  125.  
  126. write_bitmap (image, wfile, type)
  127. FBM *image;
  128. FILE *wfile;
  129. int type;
  130.   switch (type)
  131.   {
  132.     case FMT_PBM:    if (! write_pbm (image, wfile)) return (0); break;
  133.     case FMT_FBM:    if (! write_fbm (image, wfile)) return (0); break;
  134.     case FMT_FACE:    if (! write_face (image, wfile)) return (0); break;
  135.     case FMT_SUN:    if (! write_sun (image, wfile)) return (0); break;
  136.     case FMT_GIF:    if (! write_gif (image, wfile)) return (0); break;
  137.     case FMT_IFF:    if (! write_iff (image, wfile)) return (0); break;
  138.     case FMT_JPEG:    if (! write_jpeg (image, wfile, quality)) return (0); break;
  139.     case FMT_RLE:    if (! write_rle (image, wfile)) return (0); break;
  140.     case FMT_PCX:    if (! write_pcx (image, wfile)) return (0); break;
  141.     default:        fprintf (stderr,  
  142.                  "write_bitmap: unknown format type %d\n",
  143.                  type);
  144.                 return (0);
  145.   }
  146.  
  147.   return (1);
  148. }
  149.