/* subsamp.cpp * * subsamp extracts a subimage of the file in and outputs it to file out. * extraction begins at line fprec, pixel fpsam, and continues over nrec * records, nsam pixels taking every inc pixel per line from every inc * line. * * parameters * in workfile from which the image is to be extracted * out workfile to which the extracted image is to be written * fpsam pixel of the first line at which sampling is to begin * fprec line of the input file at which sampling is to begin * nsam number of pixels per line to be spanned * nrec number of records to be spanned in the sampling * inc interval at which sampling is to take place * *******/ #include <stdio.h> #include "image.h" void subsamp(IMAGE *in, IMAGE *out, int fpsam, int fprec,int incpix, int inclin); int main(int argc, char *argv[]) { int i, fpix, flin, incpix, inclin, hlen, vlen; IMAGE *in, *out; char *infile, *outfile; if (argc<2) { printf("Usage: subsamp infile outfile [first] [inc]\n"); printf("Subsample input image\n"); return -1; } infile = argv[1]; printf("input image: %s\n",infile); in = open_image(infile); if (!in) { printf("error opening input file\n"); return -1; } printf("image size: %d x %d\n",in->hlen,in->vlen); fpix = flin = 5; incpix = inclin = 3; if (argc>3) { sscanf(argv[3],"%d",&fpix); flin = fpix; } if (argc>4) { sscanf(argv[4],"%d",&incpix); inclin = incpix; } if (argc>5) sscanf(argv[5],"%d",&flin); if (argc>6) sscanf(argv[6],"%d",&inclin); /* check parameters */ if (fpix<0) fpix=0; if (flin<0) flin=0; if (incpix<1) incpix=1; if (inclin<1) inclin=1; printf("starting sample: (%d, %d)\n",fpix,flin); printf("sample spacing: %d x %d\n",incpix,inclin); /* set output image size */ hlen = vlen = 0; for (i=fpix; i<in->hlen; i += incpix) hlen++; for (i=flin; i<in->vlen; i += inclin) vlen++; outfile = argv[2]; printf("output file: %s\n",outfile); out = make_image(outfile,hlen,vlen,in->type); if (!out) { printf("error creating output file\n"); return -1; } subsamp(in,out,fpix,flin,incpix,inclin); printf("image size: %d x %d\n",out->hlen,out->vlen); return 0; } void subsamp(IMAGE *in, IMAGE *out, int fpsam, int fprec,int incpix, int inclin) { int i, k, kin, line, lino; pixel *buf, *ibuf; ibuf = make_buffer(in); buf = make_buffer(out); for (i=0;i<out->hlen;i++) buf[i]=0; /* Skip to the first desired record. * Extract pixels from input file at the specified increment, * creating one output line at a time. Output that line, then skip * lines on input file for the given increment. Continue until * all lines have been output to new file. */ lino=0; line=fprec; while ((lino<out->vlen) && (line<in->vlen)) { get_line(in,line,ibuf,in->type); k=0; kin=fpsam; while ((k<out->hlen)&&(kin<in->hlen)) { kin = fpsam + k*incpix; buf[k]=ibuf[kin]; k++; kin += incpix; } put_line(out,lino,buf,in->type); lino++; line +=inclin; } for (i=0;i<out->hlen;i++) buf[i]=0; while (lino<out->vlen) put_line(out,lino++,buf,in->type); free_buffer(buf); free_buffer(ibuf); }