Byte Swapping

Cosme Furlong
WPI-ME/CHSLT


This is the modified RTI header file used for byte-swapping
 
//
// Modified RTI image header file to do image transfer from
// Datacube (Motorola processor/Big-endian) to
// Intel processors (little-endian)
//
// Has been used and compiled with MS Visual C++ on a
// Windows based Intel platform
//
// Cosme Furlong, 1996
// WPI-ME/Laser Laboratory
//

#include <stdio.h>
#include <iostream.h>

//
// Definitions
//

#define HOLO_SIGN       "RTI"

//
// Header structures for data files
//
 
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 {
  unsigned char         signiture[4];
  unsigned char         suffix[4];
  unsigned char         dataSize;
  unsigned char         camNum;
  short int             space1;
  short int             saveMethod;
  short int             formatRevision;
  unsigned char         xMin[sizeof(short)];    // Modified from (short int) type
  unsigned char         yMin[sizeof(short)];    // Modified from (short int) type
  unsigned char         xMax[sizeof(short)];    // Modified from (short int) type
  unsigned char         yMax[sizeof(short)];    // Modified from (short int) type
  long                  annotationOffset;
  long                  dataOffset;
  char                  comment[80];
  double                xScale;
  double                yScale;
  double                zScale;
  double                xOrig;
  double                yOrig;
  double                zOrig;
  unsigned char         pUnits[20];
  unsigned char         iUnits[20];
  struct                TimeCode timeCode;
  short int             fileType;
} FileHeaderData;
 

Here is an abstract of my C-code to do byte swapping on 16/32-bit RTI images, additions / modifications / suggestions are welcome
 

//
// Abstract of program used to translate
// big-endian images to little-endian
//
// Based on program used to read RTI images
//
// Cosme Furlong
// WPI-ME/Laser Laboratory
// Written in ANSI C
//

#include <stdio.h>
#include <iostream.h>

#define HOLO_SIGN       "RTI"

 
main(argc,argv)
int argc;
char **argv;
{

        unsigned char   sbuf[sizeof(short)],
                        fbuf[sizeof(float)],
                        sbswap[sizeof(short)],
                        fbswap[sizeof(float)],
                        head_rti[10];
        int             iXmin, iYmin,
                        iXmax, iYmax,
                        iXsize, iYsize,
                        dataSize;
        long            i, size;
        short           sValue;
        FILE            *iHandle,*oHandle;

//
// Other definition variables removed for clarity
//
//
//
        if ( argc < 3 ) {
                printf("\nError.  Try: \n");
                printf(">%s input_rti(Datacube) output_rti(INTEL) \n", argv[0]);
                exit(-1);
        }

//
// Open i/o files - binary
//
        if ( (iHandle = fopen(argv[1],"r+b")) == NULL) {
                printf("Could not open INPUT file!\n");
                exit(-2);
        }

        if ( (oHandle = fopen(argv[2],"w+b")) == NULL) {
                printf("Could not open OUTPUT file!\n");
                exit(-2);
        }

//
// Read header of input file
//
        fread(head_rti,10,1,iHandle);
 
        if (strncmp(head_rti,HOLO_SIGN,strlen(HOLO_SIGN)) == 0) {
                printf("Input file is an RTI file.\n");
 
        } else {

                printf("Input file is NOT an RTI file. Exiting....\n");
                exit(-3);
        }

// Set i/o files to first byte

        fseek(iHandle,0,SEEK_SET);
        fseek(oHandle,0,SEEK_SET);

// Read RTI header

        fread(&hh,sizeof(hh),1,iHandle); // Read header of input RTI image

        iXmin = (hh.xMin[0]<<8) + (hh.xMin[1]<<0);      // Form a (short int)
        iYmin = (hh.yMin[0]<<8) + (hh.yMin[1]<<0);      // by shifting
        iXmax = (hh.xMax[0]<<8) + (hh.xMax[1]<<0);      // 0-byte, 8-bits
        iYmax = (hh.yMax[0]<<8) + (hh.yMax[1]<<0);      // to the left and add

        iXsize = iXmax - iXmin + 1;
        iYsize = iYmax - iYmin + 1;

        if ((iXsize || iYsize) < 0 ) {
                printf("Error.  Negative x/y-dimension.\n");
                exit (-3);
        }

        size     = (long)(iXsize*iYsize);
        dataSize = (int)hh.dataSize;

//
// Modify structure of output header image (not shown)
// appropriately and write 
//
        fwrite(&hh, sizeof(hh),1,oHandle);

//
// Shift and set to the 256 byte
//
        fseek(iHandle,256,SEEK_SET);
        fseek(oHandle,256,SEEK_SET);

//
// Write data
//

    switch(dataSize) 
      {
      case(16):
                
        for(i=0; i < size && !feof(iHandle); i++) {
                fread(sbswap,sizeof(short),1,iHandle);

                sbuf[0] = sbswap[1]; // Byte swaping
                sbuf[1] = sbswap[0];

                sValue = ((short)sbuf[0]<<0) + ((short)sbuf[1]<<8);
                fwrite(&sValue,sizeof(short),1,oHandle);
        }
        break;


        case(32):

        fvalue = (float *)fbuf;

        for(i=0; i < size && !feof(iHandle); i++) {
                fread(fbswap,sizeof(float),1,iHandle);

                fbuf[0] = fbswap[3]; // Swap bytes
                fbuf[1] = fbswap[2];
                fbuf[2] = fbswap[1];
                fbuf[3] = fbswap[0];

                fwrite(fValue,sizeof(float),1,oHandle);
        }
        break;

      default:

        printf("Not a data file.  Exiting...\n");
        exit(-1);
        
        break;

      } /*  switch  */


/*      CLOSE FILES AND FREE MEMORY     */

    fclose(iHandle);
    fclose(oHandle);

}


Back to top
Back to ME-593N page


[Main]



[Mechanical Engineering Department]


[CHSLT / NEST Labs]