static int read_picture(job_t *job) { int rc=0; assert(job); if (strstr(job->src.fname, ".pcx")) readpcx(job->src.fname, &job->src.p, job->cfg.verbose); else rc=readpgm(job->src.fname, &job->src.p, job->cfg.verbose); return rc; /* 1 for multiple images, -1 on error, 0 else */ }
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 ); }