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 #include <iostream>
00028 #include <stdio.h>
00029
00030 #include "vltriple.h"
00031 #include "vlvoldata.h"
00032 #include "vlvolinfo.h"
00033 #include "vlvoliterator.h"
00034 #include "vlvoldatalayout.h"
00035 #include "vlconstants.h"
00036 #include "vlgenericfactory.h"
00037
00038 #include "f3d.h"
00039
00040 typedef vlGenericFactory<vlVolFiof3d> vlVolFiof3dFactory;
00041 VL_EXPORT_COMPONENT_FACTORY( f3dfio, vlVolFiof3dFactory );
00042
00043 #define PGM 6
00044 #define PPM 5
00045
00049 vlVolFiof3d::vlVolFiof3d()
00050 {
00051 infoRef().setVersion(0, 1, 0);
00052 infoRef().setService("f3d", "OpenVL's f3d FileIO Filter");
00053 infoRef().setName("f3dFileIO");
00054
00055 m_fileExtensions.push_back("f3d");
00056 }
00057
00058
00062 vlVolFiof3d::~vlVolFiof3d()
00063 {
00064
00065 }
00066
00067
00073 const std::vector<std::string> & vlVolFiof3d::getFileExtensions() const
00074 {
00075 return (m_fileExtensions);
00076 }
00077
00078
00089 bool vlVolFiof3d::readInfo(vlVolInfo & info, FILE * const fp, const std::string & filename)
00090 {
00091 char buffer[80];
00092 int pnmType;
00093 unsigned int nx, ny;
00094 char f3dParam[80], f3dValue[80];
00095
00096 float px(1.0), py(1.0), pz(1.0);
00097
00098 vlUnit::UnitType unitType;
00099
00100
00101 unsigned int iconWidth, iconHeight;
00102
00103
00104 fgets(buffer, 80, fp);
00105 if(feof(fp))
00106 return false;
00107
00108 if(sscanf(buffer, "P%d", &pnmType) != 1){
00109 return false;
00110 }
00111
00112 if (!(pnmType == PPM || pnmType == PGM)){
00113 return false;
00114 }
00115
00116
00117 fgets(buffer, 80, fp);
00118 sscanf(buffer, "%d", &iconWidth);
00119 sscanf(buffer, "%d", &iconHeight);
00120
00121
00122 fgets(buffer, 80, fp);
00123 float fVersion(0.0);
00124 sscanf(buffer, "#!f3d %f", &fVersion);
00125 std::cout << " F3D Version : " << fVersion << std::endl;
00126
00127 if(!feof(fp))
00128 {
00129 fgets(buffer, 80, fp);
00130 }
00131
00132 while(buffer[0] == '#') {
00133 if(buffer[1] == '!') {
00134 sscanf(buffer, "%s", f3dParam);
00135
00136
00137 if(strcmp(f3dParam, "#!endian") == 0) {
00138 sscanf(buffer, "#!endian %s", f3dValue);
00139 std::cout << "Endian : " << f3dValue << std::endl;
00140 }
00141
00142 else if(strcmp(f3dParam, "#!vdim") == 0) {
00143 unsigned int a, b, c;
00144 sscanf(buffer, "#!vdim %d %d %d", &a, &b, &c);
00145 vlDim dim((uint16)a,(uint16)b,(uint16)c);
00146 info.setDim(dim);
00147 std::cout << "Read dimensions : " << info.dim() << std::endl;
00148 }
00149
00150
00151 else if(strcmp(f3dParam, "#!vtype") == 0) {
00152 sscanf(buffer, "#!vtype %s", f3dValue);
00153 if(strcmp(f3dValue, "f3dCubic") != 0)
00154 std::cout << "Grid not supported" << std::endl;
00155 else
00156 std::cout << "Grid Type : " << f3dValue << std::endl;
00157 }
00158
00159
00160 else if(strcmp(f3dParam, "#!ctype") == 0) {
00161 sscanf(buffer, "#!ctype %s", f3dValue);
00162 }
00163
00164
00165 else if(strcmp(f3dParam, "#!dtype") == 0) {
00166 sscanf(buffer, "#!dtype %s", f3dValue);
00167 if(strcmp(f3dValue, "f3dUChar") == 0)
00168 info.setDataType(UnsignedInt8);
00169 else if (strcmp(f3dValue, "f3dChar") == 0)
00170 info.setDataType(SignedInt8);
00171 else if(strcmp(f3dValue, "f3dUInt16") == 0)
00172 info.setDataType(UnsignedInt16);
00173 else if (strcmp(f3dValue, "f3dInt16") == 0)
00174 info.setDataType(SignedInt16);
00175 else if(strcmp(f3dValue, "f3dUInt32") == 0)
00176 info.setDataType(UnsignedInt32);
00177 else if (strcmp(f3dValue, "f3dInt32") == 0)
00178 info.setDataType(SignedInt32);
00179 else if (strcmp(f3dValue, "f3dFloat") == 0)
00180 info.setDataType(Float);
00181 else
00182 info.setDataType(UnknownDataType);
00183 }
00184
00185 else if(strcmp(f3dParam, "#!units") == 0) {
00186 sscanf(buffer, "#!units %s", f3dValue);
00187 if(strcmp(f3dValue, "f3dUum") == 0)
00188 unitType = vlUnit::MicroMeter;
00189 else if (strcmp(f3dValue, "f3dUmm") == 0)
00190 unitType = vlUnit::MilliMeter;
00191 else if (strcmp(f3dValue, "f3dUm") == 0)
00192 unitType = vlUnit::Meter;
00193 else unitType = vlUnit::NoUnitType;
00194
00195 std::cout << " Unit Type : " << unitType << std::endl;
00196 }
00197
00198
00199 else if(strcmp(f3dParam, "#!px") == 0) {
00200 sscanf(buffer, "#!px %f", &px);
00201 }
00202 else if(strcmp(f3dParam, "#!py") == 0) {
00203 sscanf(buffer, "#!py %f", &py);
00204 }
00205 else if(strcmp(f3dParam, "#!pz") == 0) {
00206 sscanf(buffer, "#!pz %f", &pz);
00207 }
00208 else if ((strcmp(f3dParam, "#!rx") ==0) ||
00209 (strcmp(f3dParam, "#!ry") ==0) ||
00210 (strcmp(f3dParam, "#!rz") ==0) ||
00211 (strcmp(f3dParam, "#!absMax") ==0) ||
00212 (strcmp(f3dParam, "#!absMin") ==0) ||
00213 (strcmp(f3dParam, "#!defaultMax") ==0) ||
00214 (strcmp(f3dParam, "#!defaultMin") ==0) ||
00215 (strcmp(f3dParam, "#!commLines") ==0) ||
00216 (strcmp(f3dParam, "#!comment") ==0) ||
00217 (strcmp(f3dParam, "#!last") ==0)) {
00218
00219 }
00220
00221 else {
00222 std::cout << "NOT supported : " << f3dParam << std::endl;
00223 std::cout << " Unsuported parameter" << std::endl;
00224 }
00225 }
00226 fgets(buffer, 80, fp);
00227 }
00228
00229 unsigned int maxDensityValue;
00230 sscanf(buffer, "%d", &maxDensityValue);
00231
00233 uint32 headerSize=ftell(fp);
00234
00235 headerSize+=iconWidth*iconHeight*3;
00236 info.setHeaderSize(headerSize);
00237
00238
00239 vlUnit units(px, py, pz, unitType);
00240 info.setUnits(units);
00241 std::cout << "Read units : " << info.units() << std::endl;
00242
00243 return (true);
00244 }
00245
00252 bool vlVolFiof3d::readData(vlVolInfo & info, vlVolData * const data, FILE * const fp, const std::string & filename)
00253 {
00254
00255 if(fp == 0) {
00256 std::cout << "Invalid file pointer." << std::endl;
00257 return (false);
00258 }
00259
00260 if(!info.isValid()) {
00261 std::cout << "Cannot load a volume coz volume info is not complete." << std::endl;
00262 return (false);
00263 }
00264
00265 if(data->dim() != info.dim()) {
00266 std::cout << "Invaid data storage given to readData() - wrong dimensions" << std::endl;
00267 return (false);
00268 }
00269
00270 if(data->dataType() != info.dataType()) {
00271 std::cout << "Invaid data storage given to readData() - wrong datatype" << std::endl;
00272 return (false);
00273 }
00274
00275
00276 fseek(fp, info.headerSize(), SEEK_SET);
00277
00278 bool retVal(false);
00279
00280 return (retVal);
00281 }
00282
00283 template <class T>
00284 bool vlVolFiof3d::readDataT(T & dummy, vlVolInfo & info, vlVolData * const data,
00285 FILE * const fp, const std::string & filename)
00286 {
00287
00288 vlVolIter<T> iter(data);
00289
00290 T voxel;
00291 uint32 voxelCount = info.dim().x()*info.dim().y()*info.dim().z();
00292
00293 while(1){
00294
00295 if(feof(fp)) {
00296
00297 std::cout << "EOF reached before reading all data. File is not complete." << std::endl;
00298 std::cout << voxelCount << " voxels missing from the file." << std::endl;
00299
00300 std::cout << "Filling remainder with zeros." << std::endl;
00301 voxel = 0;
00302 do {
00303 iter.set(voxel);
00304 } while (iter.nextXYZ());
00305
00306 return(true);
00307 } else {
00308
00309 fread(&voxel, sizeof(T), 1, fp);
00310
00311
00312 iter.set(voxel);
00313 if(!iter.nextXYZ()) {
00314 break;
00315 }
00316 --voxelCount;
00317
00318
00319 if(!voxelCount)
00320 break;
00321 }
00322 }
00323
00324
00325 if (!feof(fp)) {
00326
00327 std::cout << "Volume dimensions are incorrect. More data remaining in file." << std::endl;
00328 std::cout << "Skipping additional data." << std::endl;
00329
00330 return (true);
00331 }
00332
00333
00334 return (true);
00335 }
00336
00337
00346 bool vlVolFiof3d::writeInfo(const vlVolInfo & info, FILE * const fp, const std::string & filename)
00347 {
00348
00349 fprintf(fp, "P7\n%d %d %d\n", (int)info.dim().x(), (int)info.dim().y(), (int)info.dim().z());
00350 fprintf(fp, "%d\n", (int)(pow(2, info.bitsPerVoxel())-1));
00351
00352 return (true);
00353 }
00354
00364 bool vlVolFiof3d::writeData(const vlVolInfo & info, const vlVolData * data,
00365 FILE * const fp, const std::string & filename)
00366 {
00367
00368
00369 bool retValue;
00370 callFunctionOnDataType(data->dataType(), retValue, writeDataT, info, data, fp, filename);
00371
00372 return (retValue);
00373
00374 }
00375
00376 template <class T>
00377 bool vlVolFiof3d::writeDataT(T & dummy, const vlVolInfo & info, const vlVolData * data,
00378 FILE * const fp, const std::string & filename)
00379 {
00380
00381 vlVolIterConst<T> iter(data);
00382 T voxel;
00383
00384
00385 do {
00386 voxel = iter.get();
00387 fwrite(&voxel, sizeof(T), 1, fp);
00388 } while (iter.nextXYZ());
00389
00390
00391 return (true);
00392 }
00393