00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <sys/types.h>
00030 #include <time.h>
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <limits.h>
00034 #include <UT/UT_Endian.h>
00035 #include <UT/UT_DSOVersion.h>
00036 #include <UT/UT_SysClone.h>
00037 #include <IMG/IMG_Format.h>
00038 #include "IMG_Raw.h"
00039
00040 #define MAGIC 0x1234567a
00041 #define MAGIC_SWAP 0xa7654321 // Swapped magic number
00042
00043 namespace HDK_Sample {
00044 typedef struct {
00045 unsigned int magic;
00046 unsigned int xres;
00047 unsigned int yres;
00048 unsigned int model;
00049 unsigned int data;
00050 } IMG_RawHeader;
00051
00052
00053
00054
00055 class IMG_RawFormat : public IMG_Format {
00056 public:
00057 IMG_RawFormat() {}
00058 virtual ~IMG_RawFormat() {}
00059
00060 virtual const char *getFormatName() const;
00061 virtual const char *getFormatLabel() const;
00062 virtual const char *getFormatDescription() const;
00063 virtual const char *getDefaultExtension() const;
00064 virtual IMG_File *createFile() const;
00065
00066
00067
00068
00069 virtual int checkExtension(const char *filename) const;
00070 virtual int checkMagic(unsigned int) const;
00071 virtual IMG_DataType getSupportedTypes() const
00072 { return IMG_DT_ALL; }
00073 virtual IMG_ColorModel getSupportedColorModels() const
00074 { return IMG_CM_ALL; }
00075
00076
00077 virtual void getMaxResolution(unsigned &x,
00078 unsigned &y) const;
00079
00080 virtual int isReadRandomAccess() const { return 0; }
00081 virtual int isWriteRandomAccess() const { return 0; }
00082 };
00083 }
00084
00085 using namespace HDK_Sample;
00086
00087 const char *
00088 IMG_RawFormat::getFormatName() const
00089 {
00090
00091 return "Raw";
00092 }
00093
00094 const char *
00095 IMG_RawFormat::getFormatLabel() const
00096 {
00097
00098 return "Simple Raw Data";
00099 }
00100
00101 const char *
00102 IMG_RawFormat::getFormatDescription() const
00103 {
00104
00105
00106 return "Sample image format. Not very useful";
00107 }
00108
00109 const char *
00110 IMG_RawFormat::getDefaultExtension() const
00111 {
00112
00113
00114
00115 return "raw";
00116 }
00117
00118 IMG_File *
00119 IMG_RawFormat::createFile() const
00120 {
00121 return new IMG_Raw;
00122 }
00123
00124 int
00125 IMG_RawFormat::checkExtension(const char *filename) const
00126 {
00127 static const char *extensions[] = { ".raw", ".RAW", 0 };
00128 return matchExtensions(filename, extensions);
00129 }
00130
00131 int
00132 IMG_RawFormat::checkMagic(unsigned int magic) const
00133 {
00134
00135 return (magic == MAGIC || magic == MAGIC_SWAP);
00136 }
00137
00138 void
00139 IMG_RawFormat::getMaxResolution(unsigned &x, unsigned &y) const
00140 {
00141 x = UINT_MAX;
00142 y = UINT_MAX;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 IMG_Raw::IMG_Raw()
00153 {
00154 myByteSwap = 0;
00155 }
00156
00157 IMG_Raw::~IMG_Raw()
00158 {
00159 close();
00160 }
00161
00162 int
00163 IMG_Raw::open()
00164 {
00165 return readHeader();
00166 }
00167
00168
00169
00170 static const char *theTextureOptions[] = {
00171 "camera:orthowidth",
00172 "camera:zoom",
00173 "camera:projection",
00174 "image:crop",
00175 "image:window",
00176 "image:pixelaspect",
00177 "image:samples",
00178 "space:world",
00179 NULL
00180 };
00181
00182 static void
00183 writeTextureOption(const char *token, const char *value)
00184 {
00185
00186 }
00187
00188 int
00189 IMG_Raw::create(const IMG_Stat &stat)
00190 {
00191
00192 myStat = stat;
00193 if (!writeHeader())
00194 return 0;
00195
00196
00197
00198
00199
00200 for (int i = 0; theTextureOptions[i]; ++i)
00201 {
00202 const char *value;
00203 value = getOption(theTextureOptions[i]);
00204 if (value)
00205 writeTextureOption(theTextureOptions[i], value);
00206 }
00207 return true;
00208 }
00209
00210 int
00211 IMG_Raw::closeFile()
00212 {
00213
00214 if (myOS) myOS->flush();
00215
00216 return 1;
00217 }
00218
00219 static inline void
00220 swapHeader(IMG_RawHeader &header)
00221 {
00222 UTswapBytes((int *)&header, sizeof(header));
00223 }
00224
00225 int
00226 IMG_Raw::readHeader()
00227 {
00228 IMG_RawHeader header;
00229 IMG_Plane *plane;
00230
00231 if (!readBytes((char *)&header, sizeof(IMG_RawHeader)))
00232 return 0;
00233
00234 if (header.magic == MAGIC_SWAP)
00235 {
00236 myByteSwap = 1;
00237 swapHeader(header);
00238 }
00239 else if (header.magic != MAGIC)
00240 return 0;
00241
00242 myStat.setResolution(header.xres, header.yres);
00243 plane = myStat.addDefaultPlane();
00244 plane->setColorModel((IMG_ColorModel)header.model);
00245 plane->setDataType((IMG_DataType)header.data);
00246
00247
00248 return 1;
00249 }
00250
00251 int
00252 IMG_Raw::writeHeader()
00253 {
00254 IMG_RawHeader header;
00255
00256 header.magic = MAGIC;
00257 header.xres = myStat.getXres();
00258 header.yres = myStat.getYres();
00259 header.model = myStat.getPlane()->getColorModel();
00260 header.data = myStat.getPlane()->getDataType();
00261
00262 if (!myOS->write((char *)&header, sizeof(IMG_RawHeader)))
00263 return 0;
00264
00265
00266 return 1;
00267 }
00268
00269 int
00270 IMG_Raw::readScanline(int y, void *buf)
00271 {
00272 int nbytes;
00273
00274 if (y >= myStat.getYres()) return 0;
00275
00276 nbytes = myStat.bytesPerScanline();
00277 if (!readBytes((char *)buf, nbytes))
00278 return 0;
00279
00280
00281
00282 if (myByteSwap)
00283 {
00284 switch (myStat.getPlane()->getDataType())
00285 {
00286 case IMG_UCHAR: break;
00287 case IMG_FLOAT16:
00288 case IMG_USHORT:
00289 UTswapBytes((short *)buf, nbytes/sizeof(short));
00290 break;
00291 case IMG_UINT:
00292 UTswapBytes((int *)buf, nbytes/sizeof(int));
00293 break;
00294 case IMG_FLOAT:
00295 UTswapBytes((float *)buf, nbytes/sizeof(float));
00296 break;
00297 default:
00298 break;
00299 }
00300 }
00301
00302 return 1;
00303 }
00304
00305 int
00306 IMG_Raw::writeScanline(int , const void *buf)
00307 {
00308
00309
00310
00311
00312
00313 return (!myOS->write((char *)buf, myStat.bytesPerScanline())) ? 0 : 1;
00314 }
00315
00316
00317
00318
00319
00320
00321 void
00322 newIMGFormat(void *)
00323 {
00324 new IMG_RawFormat();
00325 }