home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch6_4 / chaincode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  3.3 KB  |  109 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "chainCode.h"
  4.  
  5.  
  6.  
  7. /*************************************************************/
  8. /*                                                           */
  9. /* Class constructor.                                        */
  10. /*                                                           */
  11. /*************************************************************/
  12.  
  13. chainCode::chainCode()
  14. {
  15. code = malloc(DEFAULT_CODE_LENGTH * sizeof(char));
  16. code[0] = '\0';
  17. length = DEFAULT_CODE_LENGTH;
  18. }
  19.  
  20. /*************************************************************/
  21. /*                                                           */
  22. /* Class destructor.                                         */
  23. /*                                                           */
  24. /*************************************************************/
  25.  
  26. chainCode::~chainCode()
  27. {
  28. free(code);
  29. }
  30.  
  31.  
  32. /*************************************************************/
  33. /*                                                           */
  34. /* This method appends a new code to the chain. If there     */
  35. /* is not enough memory left, the function doubles the size  */
  36. /* of the chain code.                                        */
  37. /* It receives as a parameter the new code to be added (c).  */
  38. /*                                                           */
  39. /*************************************************************/
  40.  
  41. void chainCode::add(char c)
  42. {
  43. int l = strlen(code);
  44.  
  45. if (l >= length-1){
  46.     length *= 2;
  47.     code = realloc(code, length);
  48.     }
  49. code[l] = c;
  50. code[l+1] = '\0';
  51. }
  52.  
  53.  
  54. /*****************************************************************/
  55. /*                                                               */
  56. /* This method post-processes a 4x chain code to generate a 1x   */
  57. /* chain code. A pointer to the 1x code is returned. The method  */
  58. /* uses the 4 following rules:                                   */
  59. /* CCCC ->  C :  reduce to one copy                              */
  60. /*  CCC -> {} :  eliminate                                       */
  61. /*   CC -> CC :  (ignored)                                       */
  62. /*    C ->  C :  identity                                        */
  63. /*                                                               */
  64. /*****************************************************************/
  65.  
  66. chainCode* chainCode::postProcess()
  67. {
  68. int    i = 0, j;
  69. chainCode *filtCode;
  70.  
  71. filtCode = new chainCode();
  72. while (i<length){
  73.     if (i+SCALE-1 < length){
  74.         for (j=0; j<SCALE-1; j++)
  75.             if (code[i+j] != code[i+j+1])
  76.                 break;
  77.         if (j == SCALE-1){
  78.             filtCode->add(code[i]);
  79.             i += SCALE;
  80.             continue;
  81.         }
  82.     }
  83.  
  84.     if (i+SCALE-2 < length){
  85.         for (j=0; j<SCALE-2; j++)
  86.             if (code[i+j] != code[i+j+1])
  87.                 break;
  88.         if (j == SCALE-2){
  89.             i += SCALE-1;
  90.             continue;
  91.             }
  92.         }
  93.     filtCode->add(code[i]);
  94.     i++;
  95. }
  96. return filtCode;
  97. }
  98.  
  99. /*****************************************************************/
  100. /*                                                               */
  101. /* A utility method to display the chain code                    */
  102. /*                                                               */
  103. /*****************************************************************/
  104.  
  105. void chainCode::printSelf()
  106. {
  107. printf("\n%s", code);
  108. }
  109.