home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_5.2 / LINEXY / LINEXY.C next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  4.6 KB  |  168 lines

  1. /*
  2.  * linexy.c
  3.  *
  4.  * Practical Algorithms for Image Analysis
  5.  *
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* LINEXY:      program lists x,y coordinates for TLC level 1 lines
  10.  *                    usage: linexy infile [-c] [-s] [-L]
  11.  *              Note -- this differs from the PCCFEAT (x,y) coordinate
  12.  *              output, because that program just dumps the coordinates
  13.  *              in PCC coding order, whereas LINEXY lists coordinates
  14.  *              in order along each connected line (breaks features are
  15.  *              connected).
  16.  *              KNOWN BUGS: If the image result of the (x,y) coordinates
  17.  *                      from LINEXY are compared against the original
  18.  *                      image before PCC coding using a difference program,
  19.  *                      there is an x,y shift causing a small (pixel or so)
  20.  *                      difference.
  21.  */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <images.h>
  26. #include "pcc2.h"               /* for PCC programs */
  27. extern void print_sos_lic ();
  28.  
  29. unsigned char *fcCode;          /* code storage */
  30. long nByteCode;                 /* no. bytes in code storage */
  31.  
  32. long xytoline (struct point *, long *, long *, long *);
  33. long usage (short);
  34. long
  35.   input (int, char **, short *);
  36.  
  37. main (argc, argv)
  38.      int argc;
  39.      char *argv[];
  40. {
  41.   long width, height;           /* image size */
  42.   short sumFlag;                /* flag=1 print coords; =2 summary; =0 all */
  43.   struct point *data;           /* data curve */
  44.   long nData;                   /* no. coords in data curve */
  45.   long nSegments;               /* number of segments found */
  46.   long nStructs;                /* number of line structures found */
  47.   long i;
  48.  
  49. /* user input */
  50.   if (input (argc, argv, &sumFlag) < 0)
  51.     return (-1);
  52.  
  53. /* open input PCC file */
  54.   if (pccread (argv[1], &fcCode, &nByteCode, &width, &height) == -1)
  55.     exit (1);
  56.   if (sumFlag != 1)
  57.     printf ("image size: %dx%d, PCC length = %d\n", width, height, nByteCode);
  58.  
  59. /* allocate space for data coordinate array */
  60.   if ((data = (struct point *)
  61.        calloc (nByteCode * MAXPERCODE, sizeof (long))) == NULL) {
  62.     printf ("LINEXY: not enough memory -- sorry");
  63.     return (-1);
  64.   }
  65.  
  66. /* construct tables of feature chain decodes */
  67.   pccdecodes ();
  68.  
  69. /* perform feature chain decoding */
  70.   pcc2xy (data, &nData);
  71.  
  72.   data[nData++].x = -STOPCODE;
  73.   if ((data = (struct point *)
  74.        realloc (data, nData * sizeof (struct point))) == NULL) {
  75.     printf ("LINEXY: not enough memory -- sorry");
  76.     return (-2);
  77.   }
  78.  
  79. /* find x,y coordinates for each line */
  80.   xytoline (data, &nData, &nSegments, &nStructs);
  81.  
  82. /* print information */
  83.   if (sumFlag != 1)
  84.     printf ("Summary: %d coordinates, %d segments, %d structures\n",
  85.             nData, nSegments, nStructs);
  86.  
  87.   if (sumFlag != 2) {
  88.     printf ("\n");
  89.     for (i = 0; i < nData; i++) {
  90.       if (data[i].x == -1)
  91.         printf ("\n\n");
  92.       else
  93.         printf ("(%d,%d)", data[i].x, data[i].y);
  94.     }
  95.     printf ("\n");
  96.   }
  97.  
  98.   return (0);
  99. }
  100.  
  101.  
  102.  
  103. /* USAGE:       function gives instructions on usage of program
  104.  *                    usage: usage (flag)
  105.  *              When flag is 1, the long message is given, 0 gives short.
  106.  */
  107.  
  108. long
  109. usage (flag)
  110.      short flag;                /* flag =1 for long message; =0 for short message */
  111. {
  112.  
  113. /* print short usage message or long */
  114.   printf ("USAGE: linexy infile [-c] [-s] [-L]\n");
  115.   if (flag == 0)
  116.     return (-1);
  117.  
  118.   printf ("\nlinexy lists (x,y) coordinates for each line\n");
  119.   printf ("segment in all image line structures; a line segment is\n");
  120.   printf ("between PCC features, including endpoints and junctions.\n\n");
  121.   printf ("ARGUMENTS:\n");
  122.   printf ("    infile: input filename (PCC) \n\n");
  123.   printf ("OPTIONS:\n");
  124.   printf ("        -c: display (x,y) coordinates only\n");
  125.   printf ("        -s: display summary of features only;\n");
  126.   printf ("            default is to display both coordinates and summary.\n");
  127.   printf ("        -L: print Software License for this module\n");
  128.  
  129.   return (-1);
  130. }
  131.  
  132.  
  133. /* INPUT:       function reads input parameters
  134.  *                  usage: input (argc, argv, &sumFlag)
  135.  */
  136.  
  137. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  138.  
  139. long
  140. input (argc, argv, sumFlag)
  141.      int argc;
  142.      char *argv[];
  143.      short *sumFlag;            /* flag=1 print coords; =2 print summary; =0 all */
  144. {
  145.   long n;
  146.  
  147.   if (argc == 1)
  148.     USAGE_EXIT (1);
  149.  
  150.  
  151.   *sumFlag = 0;
  152.  
  153.   for (n = 2; n < argc; n++) {
  154.     if (strcmp (argv[n], "-c") == 0)
  155.       *sumFlag = 1;
  156.     else if (strcmp (argv[n], "-s") == 0)
  157.       *sumFlag = 2;
  158.     else if (strcmp (argv[n], "-L") == 0) {
  159.       print_sos_lic ();
  160.       exit (0);
  161.     }
  162.     else
  163.       USAGE_EXIT (0);
  164.   }
  165.  
  166.   return (0);
  167. }
  168.