home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / lib / libpbm2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-01  |  1.7 KB  |  84 lines

  1. /* libpbm2.c - pbm utility library part 2
  2. *
  3. * Copyright (C) 1988 by Jef Poskanzer.
  4. *
  5. * Permission to use, copy, modify, and distribute this software and its
  6. * documentation for any purpose and without fee is hereby granted, provided
  7. * that the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation.  This software is provided "as is" without express or
  10. * implied warranty.
  11. %
  12. % Modified by    Jin, Guojun - LBL    10/1/91
  13. */
  14.  
  15. #include "pbm.h"
  16. #include "libpbm.h"
  17.  
  18. static bit
  19. pbm_getbit( file )
  20. FILE* file;
  21. {
  22. register int    ch;
  23.  
  24. do {
  25.     ch = pbm_getc( file );
  26. } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  27.  
  28. if ( ch != '0' && ch != '1' )
  29.     return    prgmerr(DEBUGANY, "junk in file where bits should be");
  30.  
  31. return    (ch == '1') ? 1 : 0;
  32. }
  33.  
  34. pbm_readmagicnumber(file)
  35. FILE* file;
  36. {
  37. register int    ich1 = getc(file),
  38.         ich2 = getc(file);
  39.     if (ich1==EOF || ich2==EOF)
  40.     return    prgmerr(DEBUGANY, "EOF / PNM read error reading magic number");
  41. return    (ich1 << 8) + ich2;
  42. }
  43.  
  44. void
  45. pbm_readpbminitrest( file, colsP, rowsP )
  46. FILE*    file;
  47. int    *colsP, *rowsP;
  48. {
  49. /* Read size. */
  50.     *colsP = pbm_getint(file);
  51.     *rowsP = pbm_getint(file);
  52. }
  53.  
  54.  
  55. pbm_readpbmrow( file, bitrow, cols, format )
  56. register FILE*    file;
  57. register bit*    bitrow;
  58. int    cols, format;
  59. {
  60. register int    col, bitshift;
  61. register unsigned char    item;
  62.  
  63.     switch (format)
  64.     {
  65.     case PBM_FORMAT:
  66.         for (col=0; col < cols; ++col)
  67.         *bitrow++ = pbm_getbit( file );
  68.     break;
  69.  
  70.     case RPBM_FORMAT:
  71.     bitshift = -1;
  72.     for (col=0; col < cols; ++col) {
  73.         if (bitshift == -1)    {
  74.         item = pbm_getrawbyte( file );
  75.         bitshift = 7;
  76.         }
  77.         *bitrow++ = (item >> bitshift--) & 1;
  78.     }
  79.     break;
  80.  
  81.     default:    return    prgmerr(DEBUGANY, "PBM read ???");
  82.     }
  83. }
  84.