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
/
g0_to_plane.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-31
|
6KB
|
208 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "llvs_per_plane.h"
#define HEADER_TYPE_LEN 16
#define HEADER_INFO_LEN 120
#define INT_STRING_LEN 10
#define BYTESIZE 8
#define IU_TESTBED_TYPE "SRI TB01IMGHDR"
void copy_substring(char* dest, char* src, int begin, int end)
{
strncpy(dest,&src[begin],end-begin);
dest[end-begin] = '\0';
return;
}
int read_int_from_string(char* src, int begin, int end)
{
char tmpstr[INT_STRING_LEN];
copy_substring(tmpstr,src,begin,end);
return((int)(atoi(tmpstr)));
}
int string_equal(char* string, char* strptr) /* string must end with '\0' */
{ /* but strptr doesn't have to */
return(!strncmp(string,strptr,strlen(string)));
}
int ceiling(int a, int b)
{
return((int)(ceil( ((float)a) / ((float)b) )));
}
void read_block(unsigned char *buffer, FILE *infile,
int numElements, int bytesPerElement)
{
int numbytes;
for (numbytes = numElements * bytesPerElement; numbytes > 0; numbytes--)
*buffer++ = getc(infile);
return;
}
void main(int argc, char *argv[])
{
char *infilename, *outfilename;
FILE *infile;
PLANE *outplane;
PLANE_INFO *outplane_info;
char headerString[HEADER_INFO_LEN+1];
char elementType[40-32+1];
unsigned char *block;
int imageXdim, imageYdim, elementSize, elementBytes, headerLen;
int blockXdim, blockYdim, blockSize, numXblocks, numYblocks;
int dx = 1, dy = -1; /* llvs plane top-bottom scan order (specified by dy) */
/* is opposite that of the RCDE */
int i, itmp, jtmp;
int bx, by, ix, iy, ixstart, iystart;
int level, backval = 0;
int pixval;
if (!(argc == 3)) {
fprintf(stderr,"Usage: g0_to_plane.c inimage.g0 outimage.plane\n");
fprintf(stderr,"Converts RCDE format images to LLVS plane files\n");
return;
}
infilename = argv[1];
outfilename = argv[2];
if ((infile = fopen(infilename,"r")) == NULL) {
perror("error opening input file");
perror(infilename);
return;
}
for (i=0 ; i < HEADER_TYPE_LEN; i++)
headerString[i] = getc(infile);
headerString[HEADER_TYPE_LEN] = '\0';
if (!(string_equal(IU_TESTBED_TYPE,headerString))) {
fprintf(stderr,"***Error reading image file \"%s\".\n",infilename);
fprintf(stderr,"***This program only converts iu testbed images.\n");
fprintf(stderr,"***File header label should be \"%s\"\n",IU_TESTBED_TYPE);
fprintf(stderr,"***but was found to be \"%s\".\n",headerString);
fclose(infile);
return;
}
rewind(infile);
for (i=0 ; i < HEADER_INFO_LEN; i++)
headerString[i] = getc(infile);
headerString[HEADER_INFO_LEN] = '\0';
copy_substring(elementType,headerString,32,40);
elementSize = read_int_from_string(headerString,56,64);
if (!((string_equal("UNSIGNED",elementType)) &&
((elementSize == 8) || (elementSize == 16)))) {
fprintf(stderr,"***Error reading image file \"%s\"\n",infilename);
fprintf(stderr,"***Can only convert images of element type\n");
fprintf(stderr,"*** (\"UNSIGNED\" 8) or (\"UNSIGNED\" 16)\n");
fprintf(stderr,"***but the element type was found to be\n");
fprintf(stderr,"*** (\"%s\" %d)\n", elementType, elementSize);
fclose(infile);
return;
}
elementBytes = (int)(elementSize / BYTESIZE);
imageXdim = read_int_from_string(headerString,64,72);
imageYdim = read_int_from_string(headerString,72,80);
/* testbed format gives the number of BYTES horizontally in block */
itmp = read_int_from_string(headerString,80,88);
blockXdim = (int) (itmp / elementBytes);
blockYdim = read_int_from_string(headerString,88,96);
numXblocks = ceiling(imageXdim,blockXdim);
numYblocks = ceiling(imageYdim,blockYdim);
itmp = read_int_from_string(headerString,112,120);
jtmp = blockXdim * blockYdim;
blockSize = ((itmp < jtmp) ? jtmp : itmp);
/* scan order */
if (string_equal("RL",&headerString[24]))
dx = (- dx);
if (string_equal("TB",&headerString[26]))
dy = (- dy);
headerLen = read_int_from_string(headerString,16,24);
/* position to beginning of pixel data */
rewind(infile);
for (i=0 ; i < headerLen; i++) getc(infile);
/* allocate LLVS plane */
itmp = ((imageYdim > imageXdim) ? imageYdim : imageXdim);
level = (int)(ceil(log10((double)itmp) / log10(2.0)));
if (new_plane(&outplane,&outplane_info,
((elementSize==8) ? LLVS_BYTE : LLVS_SHORT), /* data type */
level, /* level */
imageYdim, /* number of rows */
imageXdim, /* number of cols */
0, /* starting row */
0, /* starting col */
&backval /* background value */
) < 0) {
perror("g0_to_plane: new_plane");
fprintf(stderr,"error allocating LLVS plane\n");
fclose(infile);
return;
}
block = (unsigned char *)malloc(blockSize*elementSize);
if (block == NULL) {
perror("Error allocating block array");
fclose(infile);
return;
}
/* outer loop, read in blocks */
iystart = ((dy > 0) ? 0 : (imageYdim - 1));
for( itmp=0; itmp < numYblocks; itmp++) {
ixstart = ((dx > 0) ? 0 : (imageXdim - 1));
for( jtmp = 0; jtmp < numXblocks; jtmp++) {
read_block(block,infile,blockSize,elementBytes);
/* inner loop - copy block to image */
for( iy = iystart, by = 0; by < blockYdim; iy += dy, by++)
for( ix = ixstart, bx = 0; bx < blockXdim; ix += dx, bx++) {
/* if ((ix>=0)&&(ix<imageXdim)&&(iy>=0)&&(iy<imageYdim)) */
/* out of bounds checking is now done in set_pixel */
for(i=0,pixval=0; i < elementBytes; i++) {
pixval *= 256;
pixval += block[(by*blockXdim+bx)*elementBytes];
}
SET_PIXEL(pixval,outplane,iy,ix,(*outplane_info));
}
ixstart += dy * blockXdim;
}
iystart += dy * blockYdim;
}
/* write output LLVS plane file */
if (write_plane(outplane,outplane_info,"NIL",outfilename) < 0) {
perror("g0_to_plane: write_plane");
fprintf(stderr,"could not write plane file %s\n", outfilename);
}
fclose(infile);
return;
}