home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume19 / fbm / part03 / flrdfb.c < prev    next >
C/C++ Source or Header  |  1989-06-08  |  5KB  |  163 lines

  1. /*****************************************************************
  2.  * flrdfb.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.  * flrdfb.c: 
  9.  *
  10.  * CONTENTS
  11.  *    read_fbm (image, rfile, mstr, mlen)
  12.  *    read_hdr_fbm (image, rfile, mstr, mlen)
  13.  *
  14.  * EDITLOG
  15.  *    LastEditDate = Tue Mar  7 19:57:26 1989 - Michael Mauldin
  16.  *    LastFileName = /usr2/mlm/src/misc/fbm/flrdfb.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.  * 12-Nov-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  23.  *    Created.
  24.  *****************************************************************/
  25.  
  26. # include <stdio.h>
  27. # include <math.h>
  28. # include <ctype.h>
  29. # include "fbm.h"
  30.  
  31. /****************************************************************
  32.  * read_fbm: Read an FBM format bitmap into memory
  33.  ****************************************************************/
  34.  
  35. #ifndef lint
  36. static char *fbmid =
  37.     "$FBM flrdfb.c <0.9> 07-Mar-89  (C) 1989 by Michael Mauldin$";
  38. #endif
  39.  
  40. read_fbm (image, rfile, mstr, mlen)
  41. register FBM *image;
  42. register FILE *rfile;
  43. char *mstr;
  44. int mlen;
  45. { register int j, k, rowlen, plnlen;
  46.   register unsigned char *bmptr;
  47.  
  48.   if (!read_hdr_fbm (image, rfile, mstr, mlen))
  49.   { return (0); }
  50.  
  51.   alloc_fbm (image);
  52.  
  53.   rowlen = image->hdr.rowlen;
  54.   plnlen = image->hdr.plnlen;
  55.  
  56.   if (image->hdr.clrlen >  0)
  57.   { if (! fread (image->cm, image->hdr.clrlen, 1, rfile))
  58.     { fprintf (stderr, "can't read colormap (%d bytes)\n", image->hdr.clrlen);
  59.       return (0);
  60.     }
  61.   }
  62.  
  63.   for (k=0; k<image->hdr.planes; k++)
  64.   { bmptr = &(image->bm[k*plnlen]);
  65.  
  66.     for (j=0; j < image->hdr.rows; j++, bmptr += rowlen)
  67.     { if (fread (bmptr, 1, rowlen, rfile) != rowlen)
  68.       { fprintf (stderr, "premature EOF on input\n");
  69.         return (0);
  70.       }
  71.     }
  72.   }
  73.     
  74.   return (1);
  75. }
  76.  
  77. /****************************************************************
  78.  * read_hdr_fbm: Read an FBM format bitmap header into memory
  79.  ****************************************************************/
  80.  
  81. read_hdr_fbm (image, rfile, mstr, mlen)
  82. register FBM *image;
  83. register FILE *rfile;
  84. char *mstr;
  85. int mlen;
  86. { FBMFILEHDR file_hdr;
  87.   register char *hp;
  88.  
  89.   hp = (char *) &file_hdr;
  90.  
  91.   if (mlen > 0) strncpy (hp, mstr, mlen);
  92.  
  93.   if (! fread (hp+mlen, sizeof (file_hdr) - mlen, 1, rfile))
  94.   { perror ("read_fbm (header)"); return (0); }
  95.  
  96.   if (strncmp (FBM_MAGIC, file_hdr.magic, 2) != 0)
  97.   { fprintf (stderr, "Bad magic number, input is not an FBM image\n");
  98.     return (0);
  99.   }
  100.  
  101.   image->hdr.cols = atoi (file_hdr.cols);
  102.   image->hdr.rows = atoi (file_hdr.rows);
  103.   image->hdr.planes = atoi (file_hdr.planes);
  104.   image->hdr.bits = atoi (file_hdr.bits);
  105.   image->hdr.physbits = atoi (file_hdr.physbits);
  106.   image->hdr.rowlen = atoi (file_hdr.rowlen);
  107.   image->hdr.plnlen = atoi (file_hdr.plnlen);
  108.   image->hdr.clrlen = atoi (file_hdr.clrlen);
  109.   image->hdr.aspect = atof (file_hdr.aspect);
  110.   strncpy (image->hdr.title, file_hdr.title, FBM_MAX_TITLE);
  111.   strncpy (image->hdr.credits, file_hdr.credits, FBM_MAX_TITLE);
  112.     
  113.   if (image->hdr.cols < 1 || image->hdr.cols > 32767)
  114.   { fprintf (stderr, "Invalid number of cols (%d) on input\n",
  115.          image->hdr.cols);
  116.     return (0);
  117.   }
  118.  
  119.   if (image->hdr.rows < 1 || image->hdr.rows > 32767)
  120.   { fprintf (stderr, "Invalid number of rows (%d) on input\n",
  121.          image->hdr.rows);
  122.     return (0);
  123.   }
  124.  
  125.   if (image->hdr.planes != 1 && image->hdr.planes != 3)
  126.   { fprintf (stderr, "Invalid number of planes (%d) on input %s\n",
  127.          image->hdr.planes, "(must be 1 or 3)");
  128.     return (0);
  129.   }
  130.  
  131.   if (image->hdr.bits < 1 || image->hdr.bits > 8)
  132.   { fprintf (stderr, "Invalid number of bits (%d) on input %s\n",
  133.          image->hdr.bits, "(must be [1..8])");
  134.     return (0);
  135.   }
  136.  
  137.   if (image->hdr.physbits != 1 && image->hdr.physbits != 8)
  138.   { fprintf (stderr, "Invalid number of physbits (%d) on input %s\n",
  139.          image->hdr.physbits, "(must be 1 or 8)");
  140.     return (0);
  141.   }
  142.  
  143.   if (image->hdr.rowlen < 1 || image->hdr.rowlen > 32767)
  144.   { fprintf (stderr, "Invalid row length (%d) on input\n",
  145.          image->hdr.rowlen);
  146.     return (0);
  147.   }
  148.  
  149.   if (image->hdr.planes > 1 && image->hdr.plnlen < 1)
  150.   { fprintf (stderr, "Invalid plane length (%d) on input\n",
  151.          image->hdr.plnlen);
  152.     return (0);
  153.   }
  154.  
  155.   if (image->hdr.aspect < 0.01 || image->hdr.aspect > 100.0)
  156.   { fprintf (stderr, "Invalid aspect ratio %lg on input\n",
  157.          image->hdr.aspect);
  158.     return (0);
  159.   }
  160.  
  161.   return (1);
  162. }
  163.