Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

f3d.cpp

Go to the documentation of this file.
00001 // $Id: f3d.cpp,v 1.2 2003/10/15 16:42:28 lmanju Exp $ 
00002 //
00003 // OpenVL - A Library for working with volumetric datasets.
00004 //          http://openvl.sf.net
00005 //
00006 // Copyright (C) 2000-2002  Sarang Lakare <sarang#users.sf.net>
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems to openvl-devel@lists.sf.net or
00024 // at the OpenVL website.
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   // units in x y and z used for storing px, py, pz values
00096   float px(1.0), py(1.0), pz(1.0);
00097   // store type of physical unit.
00098   vlUnit::UnitType unitType;
00099 
00100   // image dimensions
00101   unsigned int iconWidth, iconHeight;
00102 
00103   /* get the type of the PNM */
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   // get icon dimensions.
00117   fgets(buffer, 80, fp);
00118   sscanf(buffer, "%d", &iconWidth);
00119   sscanf(buffer, "%d", &iconHeight);
00120   
00121   // Version of f3d format.
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      // std::cout << " f3dparam : " << f3dParam << std::endl;
00136 
00137       if(strcmp(f3dParam, "#!endian") == 0) {
00138          sscanf(buffer, "#!endian %s", f3dValue);
00139          std::cout << "Endian : " << f3dValue << std::endl;
00140       }
00141       // getting dimensions.
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     }// end if(vdim);
00149 
00150     // getting vtype
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     } // end if(vtype);
00158 
00159     // ctype
00160     else if(strcmp(f3dParam, "#!ctype") == 0) {
00161       sscanf(buffer, "#!ctype %s", f3dValue);
00162     }// end if(ctype);
00163 
00164     // Setting data type  in info.SetDataType()  ***TODO
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     }// end if(dtype);
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     }// end if(units);
00197 
00198     // voxel size in x, y and z direction for cubic, regular and rect grids.
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       // Do nothing. Skip as of now.
00219     }
00220 
00221     else {
00222       std::cout << "NOT supported : " << f3dParam << std::endl;
00223       std::cout << " Unsuported parameter" << std::endl;
00224     }
00225    } // end if loop.
00226    fgets(buffer, 80, fp); //  do i need to check end of file here?
00227   } //  end while loop
00228 
00229   unsigned int maxDensityValue;
00230   sscanf(buffer, "%d", &maxDensityValue);
00231 
00233   uint32 headerSize=ftell(fp);
00234   // add icon size
00235   headerSize+=iconWidth*iconHeight*3;
00236   info.setHeaderSize(headerSize);
00237   
00238   // storing units.
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   // error checking
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   // skip the header bytes
00276   fseek(fp, info.headerSize(), SEEK_SET);
00277   
00278   bool retVal(false);
00279   //callFunctionOnDataType(data->dataType(), retVal, readDataT, info, data, fp, filename);
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   // get an iterator over the volume
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     // check if its EOF
00295     if(feof(fp)) {
00296       // EOF reached before reading all voxels..
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       // fill with zeros
00300       std::cout << "Filling remainder with zeros." << std::endl;
00301       voxel = 0;
00302       do {
00303         iter.set(voxel);
00304       } while (iter.nextXYZ());
00305       // get out
00306       return(true);
00307     } else {
00308       // get a voxel from the file
00309       fread(&voxel, sizeof(T), 1, fp);
00310 
00311       // put the voxel into the volume
00312       iter.set(voxel);
00313       if(!iter.nextXYZ()) {
00314         break;
00315       }
00316       --voxelCount;
00317 
00318       // this case should never happen. iter should break out of the loop!
00319       if(!voxelCount)
00320         break;
00321     } // if-else(EOF)
00322   } // while
00323 
00324   // It has to be eof now.. if not, more data in file ..
00325   if (!feof(fp)) {
00326     // volume dimensions are incorrect.. more data in file
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   // success :)
00334   return (true);
00335 }
00336 
00337 
00346 bool vlVolFiof3d::writeInfo(const vlVolInfo & info, FILE * const fp, const std::string & filename)
00347 {
00348   // write header to file
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   // Write the data in the file as RAW data
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   // define an iterator over the volume data
00381   vlVolIterConst<T> iter(data);
00382   T voxel;
00383 
00384   // put the raw data directly into the file
00385   do {
00386     voxel = iter.get();
00387     fwrite(&voxel, sizeof(T), 1, fp);
00388   } while (iter.nextXYZ());
00389 
00390   // success :)
00391   return (true);
00392 }
00393 

Generated on Fri Mar 18 11:33:13 2005 for OpenVL by doxygen 1.3.3