Subroutines to Read/Write RTI Images

C. Furlong
WPI-ME/CHSLT

NOTE: Modifications may be required depending on the data size (in bytes of specific variables) handled by the computer used for developing (I recommend you to write a few lines of code to obtain such information - use the C-function sizeof( ); i.e. printf("Size of INT: %d\n",sizeof(int);).  Modifications, additions, recommendations are welcome !!



First, C-header structures defining RTI image-header,
/*                                                      */
/* Include file: rtihead.h                              */
/*                                                      */
/* Definition of RTI                                    */
/* images header structure: FileHeaderData              */
/*                                                      */
/* Definition of area of interest (AOI): FileData       */
/*                                                      */

#define HOLO_SIGN       "RTI"
#define PI              (3.141592654)
#define LP_FILTER       "LOW"            /* Used later on when doing */
#define MEDIAN_FILTER   "MEDIAN"         /* image filtering          */
 
struct TimeCode
{
  short int scene;   /* 0-999       */
  short int hour;    /* 0- 23       */
  short int minute;  /* 0- 59       */
  short int second;  /* 0- 59       */
  short int frame;   /* 0- 29       */
  short int field;   /* 1-  2  Note: -1 indicated non-valid field */
};

typedef struct _FileHeaderData {
  char signiture[4];
  char suffix[4];
  char dataSize;
  char camNum;
  short int space1;
  short int saveMethod;
  short int formatRevision;
  short int xMin;
  short int yMin;
  short int xMax;
  short int yMax;
  long annotationOffset;
  long dataOffset;
  char comment[80];
  double xScale;
  double yScale;
  double zScale;
  double xOrig;
  double yOrig;
  double zOrig;
  char pUnits[20];
  char iUnits[20];
  struct TimeCode timeCode;
  short int fileType;
} FileHeaderData;


/* Added additional structure.  NOT directly    */
/* used to read RTI header/data                 */

typedef struct _FileData {
  char  filename[20];
  short int xsize;
  short int ysize;
  long  offset;
  short int x1;
  short int y1;
  short int x2;
  short int y2;
  short int datasize;
  float minv;
  float maxv;
} FileData;

Subroutine to read RTI header information,
/*                                                      */
/* Subroutine: readRTIHeader()                          */
/*                                                      */
/* Reads RTI 16 and 32 bit image files header           */
/*                                                      */
/* Arguments:                                           */
/*              RTI input file name,                    */
/*              pointer to STRUCT containg image        */
/*              characteristics                         */
/*                                                      */
/* Returns:                                             */
/*              Flag of success/failure                 */
/*                                                      */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "rtihead.h"

readRTIHeader(inFile,hh)
char inFile[20];
FileHeaderData hh;
{
  char head_rti[10];
  FILE *handle_i;

  if ( (handle_i=fopen(inFile,"r"))==NULL ) {
     printf("Could not open INPUT file!\n");
     return(-1);
  }

  fread(head_rti,10,1,handle_i);
 
  if (strncmp(head_rti,HOLO_SIGN,strlen(HOLO_SIGN)) == 0) {
     printf("%s is an RTI file.\n\n",inFile);
  } else {
     printf("Input file is NOT an RTI file. Exiting....\n");
     return(-1);
  }
  
/*      Initialize input file location          */

  fseek(handle_i,0,SEEK_SET);

/*      Read 216 bytes of RTI header            */

  fread(&hh,sizeof(hh),1,handle_i);

  fclose(handle_i);

  return(1);
    
}

Subroutine to read RTI data information,
/*                                                      */
/* Subroutine: readRTIData()                            */
/*                                                      */
/* Reads RTI 16 and 32 bit data                         */
/*                                                      */
/* Arguments:                                           */
/*      inData - struct of FileData type.               */
/*                                                      */
/* Returns:                                             */
/*      pdata  - pointer to image data type(void),      */
/*      Flag of success/failure.                        */
/*                                                      */

#include <stdlib.h> 
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include "rtihead.h"
  
readRTIData(inData,pdata)
FileData inData;
void *pdata;
{
  int memsize;
  FILE *handle_i;

  if ( (handle_i=fopen(inData.filename,"r"))==NULL ) {
    printf("Could not open %s file!\n",inData.filename);
    return(-1);
  }

  printf("Working....\n");
  memsize = (inData.xsize)*(inData.ysize)*(inData.datasize)/8;
  fseek(handle_i,inData.offset,SEEK_SET);
  printf("memsize: %d, offset: %d\n\n",memsize,inData.offset);
  
  if (fread(pdata,memsize,1,handle_i) < 0) {
    printf("Read data %s error. Exiting.\n",inData.filename);
    return(-1);
  }

  printf("Done reading %s data!..\n\n",inData.filename);

  fclose(handle_i);
  return(1);
}

Subroutine to write RTI image header and data,
/*                                                      */
/* Subroutine: writeRTI()                               */
/*                                                      */
/* Reads processed data and writes a 16 or 32 bit       */
/* RTI image.                                           */
/*                                                      */
/* Arguments:                                           */
/*      inData   - struct of FileData type,             */
/*      npoint   - number of points of partial data     */
/*      indata   - pointer to image data type void.     */
/*      outFileName - name of output file.              */
/*                                                      */
/* Returns:                                             */
/*      Flag of success/failure.                        */
/*                                                      */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include "rtihead.h"

writeRTI(inData,npoint,indata,outFileName)
FileData inData;
int npoint;
void *indata;
char outFileName[20];
{

  char BYTE;
  short int dataSize;
  short iValue;
  int Cam = 1;
  int count;
  int offset;
  int i,j,position;
  int size;
  FILE *outFile;
  FileHeaderData hh;
  float fValue;
  void *pdata;

  if ( (outFile=fopen(outFileName,"w"))==NULL )  {
    printf("Could not open OUTPUT: %s file!\n",outFileName);
    return(-1);
  }

/*   Initialize and write RTI header    */

  strcpy(hh.signiture,HOLO_SIGN);
  for (i=0;i<4;i++) {
    hh.suffix[i] = '&';
  }
  dataSize = inData.datasize;
  hh.dataSize=(char)dataSize;
  hh.camNum = (char)Cam;
  hh.space1 = 0xFFFF;
  hh.saveMethod=200;
  hh.formatRevision=2;
  hh.xMin = 0; 
  hh.xMax = inData.xsize-1;
  hh.yMin = 0; 
  hh.yMax = inData.ysize-1;
  hh.annotationOffset = 0L;
  hh.dataOffset = 0L;
  for (i=0;i<80;i++) {
    hh.comment[i]='&';
  }
  hh.xScale = 1L; hh.yScale = 1L; hh.zScale = 1L;
  hh.xOrig  = 0L; hh.yOrig  = 0L; hh.zOrig  = 0L;
  for (i=0;i<20;i++) {
    hh.pUnits[i] = '&';
    hh.iUnits[i] = '&';
  }

/*      Write 216 - bytes header                */

  fseek(outFile,0,SEEK_SET);
  fwrite((void *)&hh,sizeof(hh),1,outFile); 
  fseek(outFile,sizeof(hh),SEEK_SET);

/*      Write empty header data (40 bytes)      */

  BYTE = '&';
  for (i = 0; i < (256 - sizeof(hh)); i++) {
    fwrite(&BYTE,1,1,outFile);
  }

/*      Inialize pointer to full image          */

  printf("\n");
  printf("Ready to write RTI-out file.  Data size: %d\n",dataSize);

  size = (inData.xsize)*(inData.ysize);

  switch(dataSize)
    {

      case(16):

        printf("Data size: %d\n",dataSize);

        if ((pdata = malloc(size*sizeof(short))) == NULL) {
         printf("Could not allocate memory for %s\n",outFileName);
         return(-1);
        }

        for (i = 0 ; i < size ; i++) {
          ((short*)pdata)[i] = 0.0;
        }

        offset = (inData.xsize)*(inData.y1)+inData.x1;
        printf("Size is: %d, offset is: %d\n",size,offset);
        printf("Npoint is: %d\n",npoint);

        fseek(outFile,256,SEEK_SET);
        for (i = 0 ; i < npoint ; i++) {
          iValue = ((short*)indata)[i];
          ((short*)pdata)[offset+i]=iValue;
        } 

        for (i = 0 ; i < size ; i++) {
          iValue = ((short*)pdata)[i];
          fwrite((void)&iValue,sizeof(short),1,outFile);
        }

        break;

      case(32):

        printf("Data size: %d\n",dataSize);

        if ((pdata = malloc(size*sizeof(float))) == NULL) {
         printf("Could not allocate memory for %s\n",outFileName);
         return(-1);
        }

        for (i = 0 ; i < size ; i++) {
          ((float*)pdata)[i] = 0.0;
        }
  
        offset = (inData.xsize)*(inData.y1)+inData.x1;
        printf("Size is: %d, offset is: %d\n",size,offset);
        printf("Npoint is: %d\n",npoint);

        fseek(outFile,256,SEEK_SET);
        for (i = 0 ; i < npoint ; i++) {
          fValue = ((float*)indata)[i];
          ((float*)pdata)[offset+i]=fValue;
        } 

        for (i = 0 ; i < size ; i++) {
          fValue = ((float*)pdata)[i];
          fwrite((void)&fValue,sizeof(float),1,outFile);
        }
        break;

      default:
        
        printf("Data size not supported!\n");
        return(-1);
        break;

  }

  free(pdata);
  fclose(outFile);
  return(1);

}


Back to top
Back to ME-593N page


[Main]



[Mechanical Engineering Department]


[CHSLT / NEST Labs]