Advertisement
Guest User

thinnerguohall.c

a guest
Dec 22nd, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5.  
  6. struct IterRet{
  7.     int* i;
  8.     bool b;
  9. };
  10.  
  11. struct IterRet thinningIteration(struct IterRet it, int x, int y, int iter)
  12. {
  13.     int* marker = malloc(x*y* sizeof *marker);
  14.  
  15.     for(int i= 0; i < x*y; ++i)
  16.         marker[i] = 0;
  17.  
  18.     it.b = false;
  19.     for (int i = 1; i < y-1; i++)
  20.     {
  21.         for (int j = 1; j < x-1; j++)
  22.         {
  23.             int p1 = it.i[(i)*x+(j)];
  24.             int p2 = it.i[(i-1)*x+(j)];
  25.             int p3 = it.i[(i-1)*x+(j+1)];
  26.             int p4 = it.i[(i)*x+(j+1)];
  27.             int p5 = it.i[(i+1)*x+(j+1)];
  28.             int p6 = it.i[(i+1)*x+(j)];
  29.             int p7 = it.i[(i+1)*x+(j-1)];
  30.             int p8 = it.i[(i)*x+(j-1)];
  31.             int p9 = it.i[(i-1)*x+(j-1)];
  32.  
  33.  
  34.  
  35.             int C  = ((~p2 & 0x01) & (p3 | p4)) +
  36.                      ((~p4 & 0x01) & (p5 | p6)) +
  37.                      ((~p6 & 0x01) & (p7 | p8)) +
  38.                      ((~p8 & 0x01) & (p9 | p2));
  39.             int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
  40.             int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
  41.             int A = iter == 0 ? (p2 | p3 | (~p5 & 0x01)) & p4 : (p6 | p7 | (~p9 & 0x01)) & p8;
  42.            
  43.             int B = (N1 < N2)? N1 : N2;
  44.             if (p1 == 1 && C == 1 && (B == 2 || B == 3) && A == 0)
  45.             {
  46.                 //it.b = true;
  47.                 marker[(i)*x+(j)] = 1;
  48.             }
  49.         }
  50.     }
  51.  
  52.     int sum = 0;
  53.     for(int i =0; i < x*y; i++)
  54.         it.i[i] &= (~marker[i] & 0x01);
  55.  
  56.    
  57.  
  58.     free(marker);
  59.  
  60.     return it;
  61. }
  62.  
  63.  
  64. int* thinning(int* it, int x, int y)
  65. {
  66.         for(int i= 0; i < x*y; ++i)
  67.             it[i] /= 255;
  68.        
  69.         struct IterRet base;
  70.         base.i = it;
  71.         base.b = false;
  72.  
  73.         do
  74.         {
  75.             base = thinningIteration(base, x, y, 0);
  76.             base = thinningIteration(base, x, y, 1);
  77.         }
  78.         while (base.b);
  79.        
  80.         for(int i= 0; i < x*y; ++i)
  81.             base.i[i] *= 255;
  82.  
  83.  
  84.         return base.i;
  85.     }
  86.  
  87. int main(int argc, char * argv[])
  88. {
  89.     int x, y, max;
  90.     int i = 0, j = 0;
  91.     FILE* fp = fopen("feep.pgm", "r");
  92.  
  93.     if(fp == NULL)
  94.     {
  95.         exit(EXIT_FAILURE);
  96.     }
  97.  
  98.     fscanf(fp,"P2\n%d %d\n%d\n", &x, &y, &max);
  99.  
  100.     int* src = malloc( sizeof *src * x * y);
  101.  
  102.     for(; i < y; ++i )
  103.     {    
  104.         for(; j < x; ++j)
  105.         {
  106.             fscanf(fp,"%d ", &src[i*x+j]);
  107.             src[i*x+j] *= 255/max;
  108.             printf("%3d ", src[i*x+j]);
  109.         }
  110.         fscanf(fp, "\n");
  111.         printf("\n");
  112.         j = 0;
  113.     }
  114.  
  115.     fclose(fp);
  116.  
  117.     thinning(src, x, y);
  118.  
  119.     freopen("out.pgm", "w", stdout);
  120.     printf("P2\n%d %d\n%d\n", x, y , 255);
  121.  
  122.     for(int i = 0; i < y; i++)
  123.     {
  124.         for(int j = 0; j < x; j++)
  125.            printf("%d ", src[i*x+j]);
  126.         printf("\n");
  127.     }
  128.     fclose(stdout);
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement