home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_5.1 / PCCDUMP / PCCDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  6.2 KB  |  225 lines

  1. /*
  2.  * pccdump.c
  3.  *
  4.  * Practical Algorithms for Image Analysis
  5.  *
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* PCCDUMP:     program reads PCC code, and outputs dump of raw code
  10.  *            with features shown
  11.  *                      usage: pccdump infile [-L]
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "pcc2.h"               /* header file for PCC programs */
  18. extern void print_sos_lic ();
  19.  
  20. unsigned char *fcCode;          /* code storage */
  21. long nByteCode;                 /* no. bytes in storage */
  22.  
  23. long pccdedump (short);
  24. long usage (short);
  25. long input (int, char **, short *);
  26.  
  27. main (argc, argv)
  28.      int argc;
  29.      char *argv[];
  30. {
  31.   long widthI, heightI;         /* size of image */
  32.   short featsFlag;              /* flag = 1 to print PCC feats; 0 otherwise */
  33.  
  34.   if (input (argc, argv, &featsFlag) < 0)
  35.     return (-1);
  36.  
  37. /* open input PCC file */
  38.   if (pccread (argv[1], &fcCode, &nByteCode, &widthI, &heightI) == -1)
  39.     exit (1);
  40.   printf ("image size: %dx%d, PCC length = %d\n",
  41.           widthI, heightI, nByteCode);
  42.  
  43. /* construct tables of feature chain decodes */
  44.   pccdecodes ();
  45.  
  46. /* perform feature chain decoding and write out dump of code */
  47.   printf ("PCC dump includes following information:\n");
  48.   printf ("codeword number, feature type, feature (x,y), PCC chain codes.\n");
  49.   printf ("(Locations (x,y) only displayed for start and break features.)\n");
  50.   pccdedump (featsFlag);
  51.  
  52.   return (0);
  53. }
  54.  
  55.  
  56.  
  57. /* PCCDEDUMP:   function reads PCC code from code storage, and writes
  58.  *            out code, indicating features
  59.  *                      usage: pccdedump ()
  60.  */
  61.  
  62. #include <images.h>             /* images information file */
  63.  
  64. #define MAXWRDSPERLINE 6        /* max code words per line of screen */
  65.  
  66. long
  67. pccdedump (featsFlag)
  68.      short featsFlag;           /* flag = 1 to print PCC feats; 0 otherwise */
  69. {
  70.   register long iByteCode,      /* code storage incrementor */
  71.     codeWord,                   /* code word contains up to 3 dirn.s */
  72.     x, y;                       /* feature coordinates */
  73.   long wrdsPerLine;             /* remaining code words per line of screen */
  74.  
  75. /* decode from storage region, and write out code and features */
  76.  
  77.   wrdsPerLine = MAXWRDSPERLINE;
  78.  
  79.   for (iByteCode = 0; iByteCode < nByteCode;) {
  80.     codeWord = (int) fcCode[iByteCode++];
  81.  
  82.     if (codeWord >= MINFEATCODE) {
  83.       wrdsPerLine = MAXWRDSPERLINE;
  84.  
  85.       switch (codeWord) {
  86.       case BIFCODE:
  87.         printf ("\n%5d: %5d = BIFCODE\t\t\t",
  88.                 iByteCode, codeWord);
  89.         break;
  90.       case CROSSCODE:
  91.         printf ("\n%5d: %5d = CROSSCODE\t\t\t",
  92.                 iByteCode, codeWord);
  93.         break;
  94.       case ENDCODE:            /* pop branches */
  95.         printf ("\n%5d: %5d = ENDCODE\t\t\t",
  96.                 iByteCode, codeWord);
  97.         break;
  98.       case STARTCODE:
  99.         x = (long) *(fcCode + iByteCode)
  100.           | ((long) *(fcCode + iByteCode + 1) << 8);
  101.         iByteCode += 2;
  102.         y = (long) *(fcCode + iByteCode)
  103.           | ((long) *(fcCode + iByteCode + 1) << 8);
  104.         iByteCode += 2;
  105.         printf ("\n%5d: %5d = STARTCODE (%3d,%3d)\t",
  106.                 iByteCode, codeWord, x, y);
  107.         break;
  108.       case LINEBRCODE:
  109.         x = (long) *(fcCode + iByteCode)
  110.           | ((long) *(fcCode + iByteCode + 1) << 8);
  111.         iByteCode += 2;
  112.         y = (long) *(fcCode + iByteCode)
  113.           | ((long) *(fcCode + iByteCode + 1) << 8);
  114.         iByteCode += 2;
  115.         printf ("\n%5d: %5d = LINEBRCODE (%3d, %3d)\t",
  116.                 iByteCode, codeWord, x, y);
  117.         break;
  118.       case BIFBRCODE:
  119.         x = (long) *(fcCode + iByteCode)
  120.           | ((long) *(fcCode + iByteCode + 1) << 8);
  121.         iByteCode += 2;
  122.         y = (long) *(fcCode + iByteCode)
  123.           | ((long) *(fcCode + iByteCode + 1) << 8);
  124.         iByteCode += 2;
  125.         printf ("\n%5d: %5d = BIFBRCODE (%3d, %3d)\t",
  126.                 iByteCode, codeWord, x, y);
  127.         break;
  128.       case CROSSBRCODE:
  129.         x = (long) *(fcCode + iByteCode)
  130.           | ((long) *(fcCode + iByteCode + 1) << 8);
  131.         iByteCode += 2;
  132.         y = (long) *(fcCode + iByteCode)
  133.           | ((long) *(fcCode + iByteCode + 1) << 8);
  134.         iByteCode += 2;
  135.         printf ("\n%5d: %5d = CROSSBRCODE (%3d, %3d)\t",
  136.                 iByteCode, codeWord, x, y);
  137.         break;
  138.       case LINECODE:
  139.         printf ("\n%5d: %5d = LINECODE\t\t\t",
  140.                 iByteCode, codeWord);
  141.         break;
  142.       case STOPCODE:
  143.         printf ("\n%5d: %5d = STOPCODE\n", iByteCode, codeWord);
  144.         return (0);
  145.       default:
  146.         break;
  147.       }
  148.     }
  149.     else if (!featsFlag) {
  150.       if (wrdsPerLine == 0) {
  151.         printf ("\n\t\t\t\t\t");
  152.         wrdsPerLine = MAXWRDSPERLINE;
  153.       }
  154.       printf ("%5d", codeWord);
  155.       wrdsPerLine--;
  156.     }
  157.   }
  158.  
  159.   printf ("\nPCCDECODE: missing STOPCODE in PCC storage \n");
  160.  
  161.   return (-1);
  162. }
  163.  
  164.  
  165. /* USAGE:       function gives instructions on usage of program
  166.  *                    usage: usage (flag)
  167.  *              When flag is 1, the long message is given, 0 gives short.
  168.  */
  169.  
  170. long
  171. usage (flag)
  172.      short flag;                /* flag =1 for long message; =0 for short message */
  173. {
  174.  
  175. /* print short usage message or long */
  176.   printf ("USAGE: pccdump infile [-L]\n");
  177.   if (flag == 0)
  178.     return (-1);
  179.  
  180.   printf ("\npccdump prints out PCC features and code\n");
  181.   printf ("stored in .pcc file and writes this output.\n\n");
  182.   printf ("ARGUMENTS:\n");
  183.   printf ("    infile: input filename (.pcc) containing PCC (BINARY)\n\n");
  184.   printf ("OPTIONS:\n");
  185.   printf ("        -f: FEATURES_FLAG; when set, displays only\n");
  186.   printf ("            PCC features, not chains.\n");
  187.   printf ("        -L: print Software License for this module\n");
  188.  
  189.   return (-1);
  190. }
  191.  
  192.  
  193. /* INPUT:       function reads input parameters
  194.  *                  usage: input (argc, argv, &featsFlag)
  195.  */
  196.  
  197. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  198.  
  199. long
  200. input (argc, argv, featsFlag)
  201.      int argc;
  202.      char *argv[];
  203.      short *featsFlag;          /* flag=1 to print only PCC feats; or 0 */
  204. {
  205.   long n;
  206.  
  207.   if (argc == 1)
  208.     USAGE_EXIT (1);
  209.  
  210.   *featsFlag = 0;
  211.  
  212.   for (n = 2; n < argc; n++) {
  213.     if (strcmp (argv[n], "-f") == 0)
  214.       *featsFlag = 1;
  215.     else if (strcmp (argv[n], "-L") == 0) {
  216.       print_sos_lic ();
  217.       exit (0);
  218.     }
  219.     else
  220.       USAGE_EXIT (0);
  221.   }
  222.  
  223.   return (0);
  224. }
  225.