// // Programa de binarização de imagems pgm, que recebe o nome // dos ficheiros pela linha de comando // int main(int argc, char **argv){ int height, width, i, j, datasize; unsigned char **imptr, *dp, limiar; printf("Lendo imagem '%s'...\n", argv[1]); imptr=readpgm(argv[1],&width,&height); printf("\tOk.\n\n"); datasize=width*height; if(!(dp=(unsigned char *)calloc(sizeof(char),datasize))){ fprintf(stderr,"writepgm: calloc error\n"); exit(0); } for (i=0;i<height;i++) for (j=0;j<width;j++) dp[i*width+j]=imptr[i][j]; #define LIMIAR 128 if (argc<4) { limiar = LIMIAR; printf("Nao foi indicado na linha de comandos o limiar a utilizar.\n"); printf("\tSera usado o valor por defeito %u.\n", limiar); } else limiar = (unsigned char) atoi(argv[3]); printf("Executando a funcao 'bin_img()' com o limiar %u...\n", limiar); bin_img(dp, width, height, limiar); printf("\tFim.\n\n"); for(i=0;i<height;i++) for(j=0;j<width;j++) imptr[i][j]=dp[i*width+j]; printf("Guardando o resultado no ficheiro '%s'...\n", argv[2]); writepgm(imptr,width,height,argv[2]); printf("\tOk.\n\n"); }
int main(int argn, char *argv[]) { char *inam, *onam; pix bild; int ox, oy, dx, dy, x, y, i, vvv = 0; #ifdef HAVE_PAM_H pnm_init(&argn, argv); #endif // skip options for (i = 1; i < argn; i++) { if (argv[i][0] != '-') break; if (!strcmp(argv[i], "-?")) help(); else if (!strcmp(argv[i], "-help")) help(); else if (!strcmp(argv[i], "-shrink")) vvv |= 2; else if (!strcmp(argv[i], "-pbm")) vvv |= 4; else printf("unknown option: %s\n", argv[i]); } if (argn - i != 6) help(); inam = argv[i++]; onam = argv[i++]; ox = atoi(argv[i++]); oy = atoi(argv[i++]); dx = atoi(argv[i++]); dy = atoi(argv[i++]); printf("# in=%s out=%s offs=%d,%d len=%d,%d vvv=%d\n", inam, onam, ox, oy, dx, dy, vvv); // ----- read picture if (strstr(inam, ".pbm") || strstr(inam, ".pgm") || strstr(inam, ".ppm") || strstr(inam, ".pnm") || strstr(inam, ".pam")) readpgm(inam, &bild, 1); else if (strstr(inam, ".pcx")) readpcx(inam, &bild, 1); else if (strstr(inam, ".tga")) readtga(inam, &bild, ((vvv > 1) ? 0 : 1)); else { printf("Error: unknown suffix\n"); exit(1); } if (ox < 0 || ox >= bild.x) ox = 0; if (oy < 0 || ox >= bild.y) oy = 0; if (dx <= 0 || ox + dx > bild.x) dx = bild.x - ox; if (dy <= 0 || oy + dy > bild.y) dy = bild.y - oy; if ((vvv & 2) == 2 && bild.bpp == 1) { // -shrink int x, y; printf("# shrinking PGM: offs=%d,%d len=%d,%d\n", ox, oy, dx, dy); for (y = 0; y < dy; y++) { // shrink upper border for (x = 0; x < dx; x++) if (bild.p[x + ox + (y + oy) * bild.x] < 127) break; if (x < dx) { if (y > 0) y--; oy += y; dy -= y; break; } } for (y = 0; y < dy; y++) { // shrink lower border for (x = 0; x < dx; x++) if (bild.p[ox + x + (oy + dy - y - 1) * bild.x] < 127) break; if (x < dx) { if (y > 0) y--; dy -= y; break; } } for (x = 0; x < dx; x++) { // shrink left border for (y = 0; y < dy; y++) if (bild.p[x + ox + (y + oy) * bild.x] < 127) break; if (y < dy) { if (x > 0) x--; ox += x; dx -= x; break; } } for (x = 0; x < dx; x++) { // shrink right border for (y = 0; y < dy; y++) if (bild.p[ox + dx - x - 1 + (oy + y) * bild.x] < 127) break; if (y < dy) { if (x > 0) x--; dx -= x; break; } } } printf("# final dimension: offs=%d,%d len=%d,%d bpp=%d\n", ox, oy, dx, dy, bild.bpp); /* bbg: could be changed to memmoves */ // ---- new size for (y = 0; y < dy; y++) for (x = 0; x < dx; x++) for (i = 0; i < 3; i++) bild.p[i + bild.bpp * (x + dx * y)] = bild.p[i + bild.bpp * (x + ox + (y + oy) * bild.x)]; bild.x = dx; bild.y = dy; // ---- write internal picture of textsite printf("# write %s\n", onam); if (strstr(onam, ".pbm")) writepbm(onam, &bild); else if (strstr(onam, ".pgm")) writepgm(onam, &bild); else if (strstr(onam, ".ppm")) writeppm(onam, &bild); else if (strstr(onam, ".pnm")) writepgm(onam, &bild); else printf("Error: unknown suffix"); free( bild.p ); }