home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1648 / pnmsig.c next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.2 KB  |  125 lines

  1. /* pnmsig.c - read a portable anymap produce a numeric signature
  2. **
  3. ** Copyright 1990 George Phillips
  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.  
  13. #include <stdio.h>
  14. #include "pnm.h"
  15.  
  16. #ifdef PPM
  17. #include "ppm.h"
  18. #include "libppm.h"
  19. #endif /*PPM*/
  20.  
  21. #ifdef PGM
  22. #include "pgm.h"
  23. #include "libpgm.h"
  24. #endif /*PGM*/
  25.  
  26. #ifdef PBM
  27. #include "pbm.h"
  28. #include "libpbm.h"
  29. #endif /*PBM*/
  30.  
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.     int    i;
  37.  
  38.     pm_progname = argv[0];
  39.  
  40.     if (argc < 2)
  41.         pnm_sig(NULL);
  42.     else {
  43.         for (i = 1; i < argc; i++)
  44.             pnm_sig(argv[i]);
  45.     }
  46.     exit(0);
  47. }
  48.  
  49. pnm_sig(fname)
  50. char* fname;
  51. {
  52.     FILE* ifp;
  53.     xelval maxval;
  54.     register xel *xelrow, *xP;
  55.     int rows, cols, format, row, col, sig;
  56.  
  57.     if (fname != NULL)
  58.         ifp = pm_openr(fname);
  59.     else
  60.         ifp = stdin;
  61.  
  62.     pnm_readpnminit(ifp, &cols, &rows, &maxval, &format);
  63.     xelrow = pnm_allocrow(cols);
  64.     sig = 0;
  65.  
  66.     for (row = 0; row < rows; row++) {
  67.         pnm_readpnmrow(ifp, xelrow, cols, maxval, format);
  68.         for (col = 0, xP = xelrow; col < cols; col++, xP++)
  69.             switch (format) {
  70. #ifdef PPM
  71.             case PPM_FORMAT:
  72.             case RPPM_FORMAT:
  73.                 sig += PPM_GETR(*xP) + PPM_GETG(*xP) + PPM_GETB(*xP);
  74.                 break;
  75. #endif /*PPM*/
  76.  
  77. #ifdef PGM
  78.             case PGM_FORMAT:
  79.             case RPGM_FORMAT:
  80.                 sig += (gray) PNM_GET1(*xP);
  81.                 break;
  82. #endif /*PGM*/
  83.  
  84. #ifdef PBM
  85.             case PBM_FORMAT:
  86.             case RPBM_FORMAT:
  87.                 sig += (bit) PNM_GET1(*xP);
  88.                 break;
  89. #endif /*PBM*/
  90.  
  91.             default:
  92.                 pm_error( "can't happen", 0,0,0,0,0 );
  93.             }
  94.     }
  95.  
  96.     if (fname != NULL) {
  97.         pm_close(ifp);
  98.         printf("%s: ", fname);
  99.     }
  100.     printf("%d x %d x ", cols, rows);
  101.     switch (format) {
  102. #ifdef PPM
  103.     case PPM_FORMAT:
  104.     case RPPM_FORMAT:
  105.         printf("%d colour", maxval + 1);
  106.         break;
  107. #endif /*PPM*/
  108. #ifdef PGM
  109.     case PGM_FORMAT:
  110.     case RPGM_FORMAT:
  111.         printf("%d grayscale", maxval + 1);
  112.         break;
  113. #endif /*PGM*/
  114. #ifdef PBM
  115.     case PBM_FORMAT:
  116.     case RPBM_FORMAT:
  117.         printf("2 bitmap");
  118.         break;
  119. #endif /*PBM*/
  120.     }
  121.     printf(" [%d]\n", sig);
  122.  
  123.     pnm_freerow(xelrow);
  124. }
  125.