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

  1. /* TEST FILE vectorize NOT FOR BOOK */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "chainCode.h"
  6. #include "pt2.h"
  7.  
  8. #define MAXPATHLEN 1024
  9.  
  10.  
  11.  
  12.  
  13. extern chainCode* encode(pt2 *size, char *bitmap);
  14.  
  15.  
  16. char *mes1[]={"VECTORIZE",
  17.               "by Jean-Francois Doue",
  18.               " ",
  19.               "This program transforms a bitmap image into a chain code",
  20.               "Bitmap images should be encoded in the following format:",
  21.               " ",
  22.               "x_size y_size",
  23.               "00001111000",
  24.               "00001111000",
  25.               "00001001000",
  26.               "...........",
  27.               "00001111000",
  28.               " ",
  29.               "To convert the image, simply type",
  30.               " ",
  31.               "vectorized fileName",
  32.               " ",
  33.               "The encoded file will be saved under fileName.vec",
  34.               "\n",
  35.               0};
  36.  
  37. char *mes2[]={"Error !",
  38.               "Your file cannot be opened",
  39.               0};
  40.  
  41.  
  42.  
  43. /*************************************************************/
  44. /*                                                           */
  45. /* A simple function to display messages on the screen       */
  46. /*                                                           */
  47. /*************************************************************/
  48.  
  49. void printMessage(char** mes)
  50. {
  51. int i;
  52. if (!mes)
  53.     return;
  54. while(mes[i]){
  55.     printf("\n%s",mes[i]);
  56.     i++;
  57. }
  58. }
  59.  
  60. /*************************************************************/
  61. /*                                                           */
  62. /* This function transforms a text-file containing a bitmap  */
  63. /* image into another text file containing the vectorized    */
  64. /* image.                                                    */
  65. /*                                                           */
  66. /*************************************************************/
  67.  
  68. main(int argc, char** argv)
  69. {
  70. pt2     size;
  71. int     i, fileN = 0;
  72. unsigned char   c;
  73. char        output_name[MAXPATHLEN],
  74.             *bitmap;
  75. FILE        *input,
  76.             *output;
  77. chainCode   *code;
  78.  
  79. /* make sure the user uses the right syntax */
  80. if (argc == 1){
  81.     printMessage(mes1);
  82.     exit(0);
  83. }
  84.  
  85. /* for all the specified files... */
  86. while (fileN < argc -1){
  87.     /* open the data file */
  88.     fileN++;
  89.     if ((input = fopen(argv[fileN], "r")) == NULL){
  90.         printMessage(mes2);
  91.         exit(0);
  92.     }
  93.  
  94.     /* create the .vec file */
  95.     sprintf(output_name,"%s.vec", argv[fileN]);
  96.         if ((output = fopen(output_name, "w")) == NULL){
  97.         printMessage(mes2);
  98.         exit(0);
  99.     }
  100.  
  101.     /* read x_size and y_size of the bitmap image*/
  102.     fscanf(input,"%d%d",&size.x, &size.y);
  103.     printf("\nEncoding file:%s (x=%d, y=%d)", argv[fileN], size.x, size.y);
  104.  
  105.     /* read the data from the bitmap image*/
  106.     i = 0;
  107.     bitmap = malloc(size.x * size.y * sizeof(char));
  108.     while (!feof(input)){
  109.         fscanf(input,"%c", &c);
  110.         if (c == '0' || c == '1'){
  111.             bitmap[i] = c;
  112.             i++;
  113.             }
  114.     }
  115.  
  116.     /* encode */
  117.     code = encode(&size, bitmap);
  118.     printf("\nThe encoded vector is:");
  119.     code->printSelf();
  120.     fprintf(output,"%s", code->code);
  121.     fclose(output);
  122.     fclose(input);
  123.     }
  124.  
  125. printf("\n");
  126. return 0;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.