Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- struct IterRet{
- int* i;
- bool b;
- };
- struct IterRet thinningIteration(struct IterRet it, int x, int y, int iter)
- {
- int* marker = malloc(x*y* sizeof *marker);
- for(int i= 0; i < x*y; ++i)
- marker[i] = 0;
- it.b = false;
- for (int i = 1; i < y-1; i++)
- {
- for (int j = 1; j < x-1; j++)
- {
- int p1 = it.i[(i)*x+(j)];
- int p2 = it.i[(i-1)*x+(j)];
- int p3 = it.i[(i-1)*x+(j+1)];
- int p4 = it.i[(i)*x+(j+1)];
- int p5 = it.i[(i+1)*x+(j+1)];
- int p6 = it.i[(i+1)*x+(j)];
- int p7 = it.i[(i+1)*x+(j-1)];
- int p8 = it.i[(i)*x+(j-1)];
- int p9 = it.i[(i-1)*x+(j-1)];
- int C = ((~p2 & 0x01) & (p3 | p4)) +
- ((~p4 & 0x01) & (p5 | p6)) +
- ((~p6 & 0x01) & (p7 | p8)) +
- ((~p8 & 0x01) & (p9 | p2));
- int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8);
- int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9);
- int A = iter == 0 ? (p2 | p3 | (~p5 & 0x01)) & p4 : (p6 | p7 | (~p9 & 0x01)) & p8;
- int B = (N1 < N2)? N1 : N2;
- if (p1 == 1 && C == 1 && (B == 2 || B == 3) && A == 0)
- {
- //it.b = true;
- marker[(i)*x+(j)] = 1;
- }
- }
- }
- int sum = 0;
- for(int i =0; i < x*y; i++)
- it.i[i] &= (~marker[i] & 0x01);
- free(marker);
- return it;
- }
- int* thinning(int* it, int x, int y)
- {
- for(int i= 0; i < x*y; ++i)
- it[i] /= 255;
- struct IterRet base;
- base.i = it;
- base.b = false;
- do
- {
- base = thinningIteration(base, x, y, 0);
- base = thinningIteration(base, x, y, 1);
- }
- while (base.b);
- for(int i= 0; i < x*y; ++i)
- base.i[i] *= 255;
- return base.i;
- }
- int main(int argc, char * argv[])
- {
- int x, y, max;
- int i = 0, j = 0;
- FILE* fp = fopen("feep.pgm", "r");
- if(fp == NULL)
- {
- exit(EXIT_FAILURE);
- }
- fscanf(fp,"P2\n%d %d\n%d\n", &x, &y, &max);
- int* src = malloc( sizeof *src * x * y);
- for(; i < y; ++i )
- {
- for(; j < x; ++j)
- {
- fscanf(fp,"%d ", &src[i*x+j]);
- src[i*x+j] *= 255/max;
- printf("%3d ", src[i*x+j]);
- }
- fscanf(fp, "\n");
- printf("\n");
- j = 0;
- }
- fclose(fp);
- thinning(src, x, y);
- freopen("out.pgm", "w", stdout);
- printf("P2\n%d %d\n%d\n", x, y , 255);
- for(int i = 0; i < y; i++)
- {
- for(int j = 0; j < x; j++)
- printf("%d ", src[i*x+j]);
- printf("\n");
- }
- fclose(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement