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 int
00169 IMG_Raw::create(const IMG_Stat &stat)
00170 {
00171 myStat = stat;
00172 return writeHeader();
00173 }
00174
00175 int
00176 IMG_Raw::closeFile()
00177 {
00178
00179 if (myOS) myOS->flush();
00180
00181 return 1;
00182 }
00183
00184 static inline void
00185 swapHeader(IMG_RawHeader &header)
00186 {
00187 UTswapBytes((int *)&header, sizeof(header));
00188 }
00189
00190 int
00191 IMG_Raw::readHeader()
00192 {
00193 IMG_RawHeader header;
00194 IMG_Plane *plane;
00195
00196 if (!readBytes((char *)&header, sizeof(IMG_RawHeader)))
00197 return 0;
00198
00199 if (header.magic == MAGIC_SWAP)
00200 {
00201 myByteSwap = 1;
00202 swapHeader(header);
00203 }
00204 else if (header.magic != MAGIC)
00205 return 0;
00206
00207 myStat.setResolution(header.xres, header.yres);
00208 plane = myStat.addDefaultPlane();
00209 plane->setColorModel((IMG_ColorModel)header.model);
00210 plane->setDataType((IMG_DataType)header.data);
00211
00212
00213 return 1;
00214 }
00215
00216 int
00217 IMG_Raw::writeHeader()
00218 {
00219 IMG_RawHeader header;
00220
00221 header.magic = MAGIC;
00222 header.xres = myStat.getXres();
00223 header.yres = myStat.getYres();
00224 header.model = myStat.getPlane()->getColorModel();
00225 header.data = myStat.getPlane()->getDataType();
00226
00227 if (!myOS->write((char *)&header, sizeof(IMG_RawHeader)))
00228 return 0;
00229
00230
00231 return 1;
00232 }
00233
00234 int
00235 IMG_Raw::readScanline(int y, void *buf)
00236 {
00237 int nbytes;
00238
00239 if (y >= myStat.getYres()) return 0;
00240
00241 nbytes = myStat.bytesPerScanline();
00242 if (!readBytes((char *)buf, nbytes))
00243 return 0;
00244
00245
00246
00247 if (myByteSwap)
00248 {
00249 switch (myStat.getPlane()->getDataType())
00250 {
00251 case IMG_UCHAR: break;
00252 case IMG_FLOAT16:
00253 case IMG_USHORT:
00254 UTswapBytes((short *)buf, nbytes/sizeof(short));
00255 break;
00256 case IMG_UINT:
00257 UTswapBytes((int *)buf, nbytes/sizeof(int));
00258 break;
00259 case IMG_FLOAT:
00260 UTswapBytes((float *)buf, nbytes/sizeof(float));
00261 break;
00262 default:
00263 break;
00264 }
00265 }
00266
00267 return 1;
00268 }
00269
00270 int
00271 IMG_Raw::writeScanline(int , const void *buf)
00272 {
00273
00274
00275
00276
00277
00278 return (!myOS->write((char *)buf, myStat.bytesPerScanline())) ? 0 : 1;
00279 }
00280
00281
00282
00283
00284
00285
00286 void
00287 newIMGFormat(void *)
00288 {
00289 new IMG_RawFormat();
00290 }