IMG/IMG_Raw.C
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <UT/UT_Endian.h>
#include <UT/UT_DSOVersion.h>
#include <UT/UT_SysClone.h>
#include <IMG/IMG_Format.h>
#include "IMG_Raw.h"
#define MAGIC 0x1234567a
#define MAGIC_SWAP 0xa7654321 // Swapped magic number
namespace HDK_Sample {
typedef struct {
unsigned int magic;
unsigned int xres;
unsigned int yres;
unsigned int model;
unsigned int data;
} IMG_RawHeader;
class IMG_RawFormat : public IMG_Format {
public:
IMG_RawFormat() {}
virtual ~IMG_RawFormat() {}
virtual const char *getFormatName() const;
virtual const char *getFormatLabel() const;
virtual const char *getFormatDescription() const;
virtual const char *getDefaultExtension() const;
virtual IMG_File *createFile() const;
virtual int checkExtension(const char *filename) const;
virtual int checkMagic(unsigned int) const;
virtual IMG_DataType getSupportedTypes() const
{ return IMG_DT_ALL; }
virtual IMG_ColorModel getSupportedColorModels() const
{ return IMG_CM_ALL; }
virtual void getMaxResolution(unsigned &x,
unsigned &y) const;
virtual int isReadRandomAccess() const { return 0; }
virtual int isWriteRandomAccess() const { return 0; }
};
}
using namespace HDK_Sample;
const char *
IMG_RawFormat::getFormatName() const
{
return "Raw";
}
const char *
IMG_RawFormat::getFormatLabel() const
{
return "Simple Raw Data";
}
const char *
IMG_RawFormat::getFormatDescription() const
{
return "Sample image format. Not very useful";
}
const char *
IMG_RawFormat::getDefaultExtension() const
{
return "raw";
}
IMG_File *
IMG_RawFormat::createFile() const
{
return new IMG_Raw;
}
int
IMG_RawFormat::checkExtension(const char *filename) const
{
static const char *extensions[] = { ".raw", ".RAW", 0 };
return matchExtensions(filename, extensions);
}
int
IMG_RawFormat::checkMagic(unsigned int magic) const
{
return (magic == MAGIC || magic == MAGIC_SWAP);
}
void
IMG_RawFormat::getMaxResolution(unsigned &x, unsigned &y) const
{
x = UINT_MAX;
y = UINT_MAX;
}
IMG_Raw::IMG_Raw()
{
myByteSwap = 0;
}
IMG_Raw::~IMG_Raw()
{
close();
}
int
IMG_Raw::open()
{
return readHeader();
}
static const char *theTextureOptions[] = {
"camera:orthowidth",
"camera:zoom",
"camera:projection",
"image:crop",
"image:window",
"image:pixelaspect",
"image:samples",
"space:world",
NULL
};
static void
writeTextureOption(const char *token, const char *value)
{
}
int
IMG_Raw::create(const IMG_Stat &stat)
{
myStat = stat;
if (!writeHeader())
return 0;
for (int i = 0; theTextureOptions[i]; ++i)
{
const char *value;
value = getOption(theTextureOptions[i]);
if (value)
writeTextureOption(theTextureOptions[i], value);
}
return true;
}
int
IMG_Raw::closeFile()
{
if (myOS) myOS->flush();
return 1;
}
static inline void
swapHeader(IMG_RawHeader &header)
{
UTswapBytes((int *)&header, sizeof(header));
}
int
IMG_Raw::readHeader()
{
IMG_RawHeader header;
IMG_Plane *plane;
if (!readBytes((char *)&header, sizeof(IMG_RawHeader)))
return 0;
if (header.magic == MAGIC_SWAP)
{
myByteSwap = 1;
swapHeader(header);
}
else if (header.magic != MAGIC)
return 0;
myStat.setResolution(header.xres, header.yres);
plane = myStat.addDefaultPlane();
plane->setColorModel((IMG_ColorModel)header.model);
plane->setDataType((IMG_DataType)header.data);
return 1;
}
int
IMG_Raw::writeHeader()
{
IMG_RawHeader header;
header.magic = MAGIC;
header.xres = myStat.getXres();
header.yres = myStat.getYres();
header.model = myStat.getPlane()->getColorModel();
header.data = myStat.getPlane()->getDataType();
if (!myOS->write((char *)&header, sizeof(IMG_RawHeader)))
return 0;
return 1;
}
int
IMG_Raw::readScanline(int y, void *buf)
{
int nbytes;
if (y >= myStat.getYres()) return 0;
nbytes = myStat.bytesPerScanline();
if (!readBytes((char *)buf, nbytes))
return 0;
if (myByteSwap)
{
switch (myStat.getPlane()->getDataType())
{
case IMG_UCHAR: break;
case IMG_FLOAT16:
case IMG_USHORT:
UTswapBytes((short *)buf, nbytes/sizeof(short));
break;
case IMG_UINT:
UTswapBytes((int *)buf, nbytes/sizeof(int));
break;
case IMG_FLOAT:
UTswapBytes((float *)buf, nbytes/sizeof(float));
break;
default:
break;
}
}
return 1;
}
int
IMG_Raw::writeScanline(int , const void *buf)
{
return (!myOS->write((char *)buf, myStat.bytesPerScanline())) ? 0 : 1;
}
void
newIMGFormat(void *)
{
new IMG_RawFormat();
}