Commit b1c83e6a authored by Ken Martin's avatar Ken Martin
Browse files

better byte swapping

parent bb60a08a
......@@ -42,8 +42,9 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
// Description:
// Swap four byte word.
void vtkByteSwap::Swap4(char *mem_ptr1)
void vtkByteSwap::Swap4BE(char *mem_ptr1)
{
#ifndef WORDS_BIGENDIAN
char one_byte;
one_byte = mem_ptr1[0];
......@@ -53,4 +54,74 @@ void vtkByteSwap::Swap4(char *mem_ptr1)
one_byte = mem_ptr1[1];
mem_ptr1[1] = mem_ptr1[2];
mem_ptr1[2] = one_byte;
#endif
}
// Description:
// Swap bunch of bytes. Num is the number of four byte words to swap.
void vtkByteSwap::Swap4BERange(char *mem_ptr1,int num)
{
#ifndef WORDS_BIGENDIAN
char one_byte;
char *pos;
int i;
pos = mem_ptr1;
for (i = 0; i < num; i++)
{
one_byte = pos[0];
pos[0] = pos[3];
pos[3] = one_byte;
one_byte = pos[1];
pos[1] = pos[2];
pos[2] = one_byte;
pos = pos + 4;
}
#endif
}
// Description:
// Swap four byte word.
void vtkByteSwap::Swap4LE(char *mem_ptr1)
{
#if WORDS_BIGENDIAN
char one_byte;
one_byte = mem_ptr1[0];
mem_ptr1[0] = mem_ptr1[3];
mem_ptr1[3] = one_byte;
one_byte = mem_ptr1[1];
mem_ptr1[1] = mem_ptr1[2];
mem_ptr1[2] = one_byte;
#endif
}
// Description:
// Swap bunch of bytes. Num is the number of four byte words to swap.
void vtkByteSwap::Swap4LERange(char *mem_ptr1,int num)
{
#ifdef WORDS_BIGENDIAN
char one_byte;
char *pos;
int i;
pos = mem_ptr1;
for (i = 0; i < num; i++)
{
one_byte = pos[0];
pos[0] = pos[3];
pos[3] = one_byte;
one_byte = pos[1];
pos[1] = pos[2];
pos[2] = one_byte;
pos = pos + 4;
}
#endif
}
......@@ -90,6 +90,8 @@ void vtkSTLReader::Execute()
}
else
{
fclose(fp);
fp = fopen(this->Filename, "rb");
if ( this->ReadBinarySTL(fp,newPts,newPolys) ) return;
}
......@@ -163,7 +165,7 @@ int vtkSTLReader::ReadBinarySTL(FILE *fp, vtkFloatPoints *newPts, vtkCellArray *
//
fread (header, 1, 80, fp);
fread (&ulint, 1, 4, fp);
swap.Swap4 (&ulint);
swap.Swap4LE(&ulint);
//
// Many .stl files contain bogus count. Hence we will ignore and read
// until end of file.
......@@ -177,23 +179,23 @@ int vtkSTLReader::ReadBinarySTL(FILE *fp, vtkFloatPoints *newPts, vtkCellArray *
{
fread(&ibuff2,2,1,fp); //read extra junk
swap.Swap4 (facet.n);
swap.Swap4 (facet.n+1);
swap.Swap4 (facet.n+2);
swap.Swap4LE (facet.n);
swap.Swap4LE (facet.n+1);
swap.Swap4LE (facet.n+2);
swap.Swap4 (facet.v1);
swap.Swap4 (facet.v1+1);
swap.Swap4 (facet.v1+2);
swap.Swap4LE (facet.v1);
swap.Swap4LE (facet.v1+1);
swap.Swap4LE (facet.v1+2);
pts[0] = newPts->InsertNextPoint(facet.v1);
swap.Swap4 (facet.v2);
swap.Swap4 (facet.v2+1);
swap.Swap4 (facet.v2+2);
swap.Swap4LE (facet.v2);
swap.Swap4LE (facet.v2+1);
swap.Swap4LE (facet.v2+2);
pts[1] = newPts->InsertNextPoint(facet.v2);
swap.Swap4 (facet.v3);
swap.Swap4 (facet.v3+1);
swap.Swap4 (facet.v3+2);
swap.Swap4LE (facet.v3);
swap.Swap4LE (facet.v3+1);
swap.Swap4LE (facet.v3+2);
pts[2] = newPts->InsertNextPoint(facet.v3);
newPolys->InsertNextCell(3,pts);
......
......@@ -153,7 +153,7 @@ void vtkSTLWriter::WriteBinarySTL(vtkPoints *pts, vtkCellArray *polys)
fwrite (header, 1, 80, fp);
ulint = (unsigned long int) polys->GetNumberOfCells();
swap.Swap4(&ulint);
swap.Swap4LE(&ulint);
fwrite (&ulint, 1, 4, fp);
//
// Write out triangle polygons. In not a triangle polygon, only first
......@@ -166,19 +166,19 @@ void vtkSTLWriter::WriteBinarySTL(vtkPoints *pts, vtkCellArray *polys)
v3 = pts->GetPoint(indx[2]);
poly.ComputeNormal(pts, npts, indx, n);
swap.Swap4(n); swap.Swap4(n+1); swap.Swap4(n+2);
swap.Swap4LE(n); swap.Swap4LE(n+1); swap.Swap4LE(n+2);
fwrite (n, 4, 3, fp);
n[0] = v1[0]; n[1] = v1[1]; n[2] = v1[2];
swap.Swap4(n); swap.Swap4(n+1); swap.Swap4(n+2);
swap.Swap4LE(n); swap.Swap4LE(n+1); swap.Swap4LE(n+2);
fwrite (n, 4, 3, fp);
n[0] = v2[0]; n[1] = v2[1]; n[2] = v2[2];
swap.Swap4(n); swap.Swap4(n+1); swap.Swap4(n+2);
swap.Swap4LE(n); swap.Swap4LE(n+1); swap.Swap4LE(n+2);
fwrite (n, 4, 3, fp);
n[0] = v3[0]; n[1] = v3[1]; n[2] = v3[2];
swap.Swap4(n); swap.Swap4(n+1); swap.Swap4(n+2);
swap.Swap4LE(n); swap.Swap4LE(n+1); swap.Swap4LE(n+2);
fwrite (n, 4, 3, fp);
fwrite (&ibuff2, 2, 1, fp);
......
......@@ -39,6 +39,7 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "vtkUnstructuredGridReader.hh"
#include "vtkByteSwap.hh"
vtkUnstructuredGridReader::vtkUnstructuredGridReader()
{
......@@ -151,6 +152,7 @@ void vtkUnstructuredGridReader::Execute()
vtkCellArray *cells=NULL;
int *types=NULL;
vtkUnstructuredGrid *output=(vtkUnstructuredGrid *)this->Output;
vtkByteSwap swap;
vtkDebugMacro(<<"Reading vtk unstructured grid...");
......@@ -234,6 +236,7 @@ void vtkUnstructuredGridReader::Execute()
vtkErrorMacro(<<"Error reading binary cell types!");
return;
}
swap.Swap4BERange(types,ncells);
}
else //ascii
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment