home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
vis-ftp.cs.umass.edu
/
vis-ftp.cs.umass.edu.tar
/
vis-ftp.cs.umass.edu
/
pub
/
Software
/
ASCENDER
/
ascendMar8.tar
/
UMass
/
BoldtNew
/
LLVS
/
partition.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-31
|
10KB
|
279 lines
/* partition.c
* break an image into (possibly overlapping) chunks
* ------------------------------------------------------------------
* was extractpieces.c - Program to extract 512x512 pieces from a huge image
* Created by Robert Heller on Wed Feb 5 08:51:16 1992
* ------------------------------------------------------------------
* Modification History:
* Dec 29 1993 - Bob Collins - Make 266x266 pieces that overlap by 10
* pixels. Renamed file to overlappingpieces.c
* Nov 12 1994 - Bob Collins - added optional arguments to specify
* minrow, mincol, maxrow, maxcol, width, and overlap.
* Feb 17 1994 - Bob Collins - added optional arguments -RC, -XY
* to specify type of coordinate system (if -XY, then bounding
* box coordinates are interpreted as minx, miny, maxx, maxy.
* Renamed file to partition.c
* ------------------------------------------------------------------
* Contents:
* ------------------------------------------------------------------
*
* Copyright (c) 1992 by University of Massachusetts
* All Rights Reserved
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "llvs_per_plane.h"
extern void extract_plane();
/*------------------------------------------------------------------*/
int usage_abort() /***** ONLINE HELP *****/
{
printf("partition - break a large image into (possibly overlapping) chunks\n\n");
printf("usage: partition planefile [-RC] [minrow mincol maxrow maxcol [width overlap]]\n");
printf("...or: partition planefile [-XY] [minx miny maxx maxy [width overlap]]\n");
printf("where\n");
printf(" planefile is an LLVS plane file\n");
printf(" -RC specifies a row-column coordinate system (default)\n");
printf(" -XY specifies an X-Y coordinate system\n");
printf(" minrow mincol maxrow maxcol specify a subimage for cropping\n");
printf(" (if -XY, these are interpreted as minx miny maxx maxy)\n");
printf(" The actual subimage boundaries will correspond to the\n");
printf(" intersection of this rectangle with the image boundaries.\n");
printf(" width is the pixel width/height of each chunk (default 256)\n");
printf(" overlap is the pixel overlap between chunks (default 0)\n");
exit(EINVAL);
}
/*------------------------------------------------------------------*/
void main (int argc, char *argv[])
{
static PLANE *inpl, *outpl, *blockpl;
static PLANE_INFO *inpl_info, *outpl_info, *blockpl_info;
static LIMITS limits;
static char *inassoc, outfile[256], basename[256], infile[256];
FILE *infofile;
register char *p;
int error;
int rowcol_coords = TRUE; /* default is row-col coordinates */
register int row, col, drow, dcol, rowdim, coldim;
int firstrow, firstcol, lastrow, lastcol; /* boundary of full image */
int minrow, mincol, maxrow, maxcol; /* boundary of subimage */
int minx, miny, maxx, maxy; /* for coversion of XY to RC */
int width = 256, overlap = 0, blockdim;
int argnum, wid, hei, newplane = FALSE;
int col_numblks, row_numblks, itmp, jtmp;
argnum = 1;
if (argc > argnum) /* PLANE FILENAME ARGUMENT */
strcpy(infile,argv[argnum++]);
else
usage_abort();
if (argc > argnum) { /* OPTIONAL COORDINATE SYSTEM SPECIFICATION */
if ((!strcmp(argv[argnum],"-RC")) || (!strcmp(argv[argnum],"-rc"))) {
rowcol_coords = TRUE;
argnum++;
}
else if ((!strcmp(argv[argnum],"-XY")) || (!strcmp(argv[argnum],"-xy"))) {
rowcol_coords = FALSE;
argnum++;
}
}
if (argc > argnum) { /* OPTIONAL SUBIMAGE RECTANGLE */
minrow = atoi(argv[argnum++]);
mincol = atoi(argv[argnum++]);
maxrow = atoi(argv[argnum++]);
maxcol = atoi(argv[argnum++]);
}
else {
minrow = -999999;
maxrow = 999999;
mincol = -999999;
maxcol = 999999;
}
if (argc > argnum) { /* OPTIONAL BLOCKSIZE AND OVERLAP */
width = atoi(argv[argnum++]);
overlap = atoi(argv[argnum++]);
}
if (argc > argnum) usage_abort();
p = strrchr(infile,'.'); /* READ IN PLANE FILE IMAGE */
if (p == NULL) strcat(infile,".plane");
else if (strchr(p,'/') != NULL) strcat(infile,".plane");
if (read_plane(&inpl,&inpl_info,&inassoc,infile) < 0) {
error = errno;
perror("partition: read_plane");
fprintf(stderr,"partition: read_plane: could not read in plane %s\n",
infile);
exit(error);
}
/* IMAGE BOUNDARIES */
firstcol = inpl_info->column_location;
lastcol = inpl_info->column_location + inpl_info->column_dimension - 1;
firstrow = inpl_info->row_location;
lastrow = inpl_info->row_location + inpl_info->row_dimension - 1;
if (!(rowcol_coords)) {
/* INTERNAL PROCESSING USES ROW-COL COORDINATES */
/* CONVERT MINX MINY MAXX MAXY TO MINROW, MINCOL, MAXROW, MAXCOL */
minx = minrow; miny = mincol; maxx = maxrow; maxy = maxcol;
minrow = lastrow - maxy; mincol = minx;
maxrow = lastrow - miny; maxcol = maxx;
}
/* INTERSECT SUBIMAGE WITH ACTUAL IMAGE BOUNDARIES */
minrow = MAX(minrow,firstrow);
mincol = MAX(mincol,firstcol);
maxrow = MIN(maxrow,lastrow);
maxcol = MIN(maxcol,lastcol);
/* COMPUTE BLOCK INFORMATION */
rowdim = maxrow - minrow + 1;
coldim = maxcol - mincol + 1;
blockdim = width - overlap;
row_numblks = (int)(rowdim / blockdim);
if ((rowdim < blockdim) || ((rowdim % blockdim) > overlap))
row_numblks++;
col_numblks = (int)(coldim / blockdim);
if ((coldim < blockdim) || ((coldim % blockdim) > overlap))
col_numblks++;
/* PREALLOCATE A PLANE FOR THE MAJORITY OF IMAGE BLOCKS */
if (new_plane(&blockpl,&blockpl_info,inpl_info->datatype,inpl_info->level,
(blockdim+overlap),(blockdim+overlap),0,0,
&inpl_info->background) < 0) {
error = errno;
perror("partition: new_plane");
fprintf(stderr,"partition: new_plane: could not allocate plane\n");
exit(error);
}
strcpy(basename,infile); /* INIT FILENAME STRING */
p = strrchr(basename,'.');
if (p != NULL) *p = '\0';
limits.level = inpl_info->level; /* INIT PLANE LIMITS STRUCTURE */
limits.deltarow = 1;
limits.deltacol = 1;
/* PRINT IMAGE STATS TO INFO FILE */
sprintf(outfile,"%s.info",basename);
if ((infofile = fopen(outfile,"w")) == NULL) {
error = errno;
perror("partition: fopen");
fprintf(stderr,"partition: error opening image info file\n");
fprintf(stderr,"partition: info filename was %s\n",outfile);
exit(error);
}
fprintf(infofile,"image: %s\n",basename);
fprintf(infofile,"image minrow: %d\n",firstrow);
fprintf(infofile,"image maxrow: %d\n",lastrow);
fprintf(infofile,"image mincol: %d\n",firstcol);
fprintf(infofile,"image maxcol: %d\n",lastcol);
fprintf(infofile,"image numrows: %d\n",lastrow-firstrow+1);
fprintf(infofile,"image numcols: %d\n",lastcol-firstcol+1);
fprintf(infofile,"crop minrow: %d\n",minrow);
fprintf(infofile,"crop maxrow: %d\n",maxrow);
fprintf(infofile,"crop mincol: %d\n",mincol);
fprintf(infofile,"crop maxcol: %d\n",maxcol);
fprintf(infofile,"crop numrows: %d\n",maxrow-minrow+1);
fprintf(infofile,"crop numcols: %d\n",maxcol-mincol+1);
fprintf(infofile,"block width: %d\n",width);
fprintf(infofile,"block overlap: %d\n",overlap);
fprintf(infofile,"row numblocks: %d\n",row_numblks);
fprintf(infofile,"col numblocks: %d\n",col_numblks);
fclose(infofile);
/* MAIN LOOP - CUT UP THE PIECES AND WRITE THEM OUT */
row = (rowcol_coords ? minrow : (maxrow - blockdim - overlap + 1));
drow = (rowcol_coords ? blockdim : (- blockdim));
for( itmp=0; itmp < row_numblks; itmp++, row+=drow) {
col = mincol;
dcol = blockdim;
for( jtmp=0; jtmp < col_numblks; jtmp++, col+=dcol) {
limits.startrow = MAX(minrow, row);
limits.startcol = MAX(mincol, col);
limits.endrow = MIN(maxrow, (row + blockdim + overlap - 1));
limits.endcol = MIN(maxcol,(col + blockdim + overlap - 1));
hei = limits.endrow - limits.startrow + 1;
wid = limits.endcol - limits.startcol + 1;
if ((hei == (blockdim + overlap)) & (wid == (blockdim + overlap))) {
outpl = blockpl; /* USE PREALLOCATED BLOCK */
outpl_info = blockpl_info;
newplane = FALSE;
}
else { /* CUSTOM-MAKE ONE OF THE RIGHT SIZE */
newplane = TRUE;
if (new_plane(&outpl,&outpl_info,inpl_info->datatype,inpl_info->level,
hei,wid,0,0,&inpl_info->background) < 0) {
error = errno;
perror("partition: new_plane");
fprintf(stderr,"partition: new_plane: could not allocate plane\n");
fprintf(stderr,"partition: size was %d by %d\n", hei, wid);
exit(error);
}
}
/* EXTRACT BLOCK FROM IMAGE */
outpl_info->row_location = limits.startrow;
outpl_info->column_location = limits.startcol;
memset(outpl,0,plane_size(outpl_info));
extract_plane(inpl,inpl_info,outpl,outpl_info,&limits);
/*
printf("Block %d,%d to %d,%d, (hei=%d,wid=%d)\n",
(int)limits.startrow, (int)limits.startcol,
(int)limits.endrow, (int)limits.endcol,
hei,wid);
*/
/* TAG OUTPUT FILE NAME WITH BLOCK LOCATION */
if (rowcol_coords)
sprintf(outfile,"%sR%dC%d.plane",basename,
(int)limits.startrow, (int)limits.startcol);
else
sprintf(outfile,"%sX%dY%d.plane",basename,
(int)limits.startcol, (int)(lastrow - limits.endrow));
/* WRITE OUT THE BLOCK SUBPLANE */
outpl_info->row_location = 0; /* old hackery, not sure why */
outpl_info->column_location = 0;
if (write_plane(outpl,outpl_info,"NIL",outfile) < 0) {
error = errno;
perror("partition: write_plane");
fprintf(stderr,"partition: write_plane: could not ");
fprintf(stderr,"write plane file %s\n", outfile);
exit(error);
}
if (newplane) {
free(outpl);
free(outpl_info);
}
}
}
return;
}