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

vlvolume.cpp

Go to the documentation of this file.
00001 // $Id: vlvolume.cpp,v 1.39 2004/08/19 11:14:33 slfrank 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 <stdio.h>
00028 #include <string.h>
00029 #include <queue>
00030 #include <math.h>
00031 #include "vlmacros.h"
00032 #include "vlvolfiofilter.h"
00033 #include "vlcolor.h"
00034 #include "vltriple.h"
00035 #include "vlvolinfo.h"
00036 #include "vlvoliterator.h"
00037 #include "vltrader.h"
00038 #include "vlkernel.h"
00039 #include "vlvoldata_linear.h"
00040 #include "vlvolume.h"
00041 #include "vlvoldatalayoutplugin.h"
00042 #define DEBUG
00043 
00051 vlVolume::vlVolume(const vlDim & dim, const vlDataType dataType, const vlUnit & units)
00052 {
00053    m_pVolData = 0L;
00054    m_layoutPlugin = 0L;
00055 
00056    resizeData(dim, dataType, units, "Linear");
00057 
00058    std::string name("noname");
00059    std::string origFileName;
00060    vlDim origFileDim(1,1,1);
00061    m_extraInfo.add(name, "name", "A name for the volume", true);
00062    m_extraInfo.add(origFileName, "origFilename", "The file from which this volume was loaded");
00063    // m_extraInfo.add(origFileDim, "origFileDim", "The dimensions of file from which this volume was loaded");
00064 }
00065 
00066 
00074 vlVolume::vlVolume(const vlDim & dim, const std::string & layout, const vlDataType dataType,
00075      const vlUnit & units)
00076 {
00077    m_pVolData = 0L;
00078    m_layoutPlugin = 0L;
00079 
00080    resizeData(dim, dataType, units, layout);
00081 
00082    std::string name("noname");
00083    std::string origFileName;
00084    vlDim origFileDim(1,1,1);
00085    m_extraInfo.add(name, "name", "A name for the volume", true);
00086    m_extraInfo.add(origFileName, "origFilename", "The file from which this volume was loaded");
00087    //m_extraInfo.add(origFileDim, "origFileDim", "The dimensions of file from which this volume was loaded");
00088 }
00089 
00090 
00094 vlVolume::~vlVolume()     
00095 {
00096    // release data
00097    if(m_pVolData)
00098       delete m_pVolData;
00099    if(m_layoutPlugin)
00100       delete m_layoutPlugin;
00101 }
00102 
00103 
00116 bool vlVolume::readInfo(const std::string & filename, vlVolInfo & info, bool useFileExt)
00117 {
00118    // pass an empty format string.
00119    std::string format;
00120    return(readInfo(filename, info, format, useFileExt));
00121 }
00122 
00123 
00137 bool vlVolume::readInfo(const std::string & filename, vlVolInfo & info, const std::string & fileFormat, bool useFileExt)
00138 {
00139    // try to open the file
00140    FILE *fp = fopen(filename.c_str(), "r");
00141    if(!fp) {
00142 #ifdef DEBUG
00143 std::cout << "Unable to open file " << filename << " for reading." << std::endl;
00144 #endif
00145       return(false);
00146    }
00147 
00148    // make sure vlKernel is initialized
00149    vlKernel::instance();
00150 
00151    // temp variable to store info
00152    vlVolInfo volInfo;
00153 
00154    // get all FileIO plugins
00155 
00156    //  kore::ServiceProvider **filters = kore::Kernel::instance()->serviceManager()->registeredProviders("OpenVL/FileIO");
00157    std::vector<vlPlugin*> plugins = vlKernel::trader()->getPluginsForGroup("FileIO");
00158    vlVolFioFilter *filter;
00159    vlPlugin *plugin;
00160    unsigned int i;
00161    std::vector<vlPlugin*>::iterator itr = plugins.begin();
00162 
00163    // loop over the filters to find the filter that provides IO capability for the given format
00164    for(; itr!=plugins.end(); ++itr) {
00165       plugin = *itr;
00166 
00167 #ifdef DEBUG
00168  std::cout << "Trying filter [Name:" << plugin->info().name() << std::flush;
00169 #endif
00170       filter = (vlVolFioFilter*) (plugin);
00171       // make sure the pointer is valid
00172       if(!filter) {
00173 #ifdef DEBUG
00174 std::cout << "FAILED" << std::endl;
00175 #endif
00176          continue;
00177       }
00178 
00179 #ifdef DEBUG
00180 std::cout << ",Format:" << filter->info().service() << "] ... " << std::flush;
00181 #endif
00182 
00183       // Check if file format is specified
00184       if(fileFormat.size()) {
00185          // if file format is specified, then only the plugins of that file format will
00186          // be used : no autodetection.
00187 
00188          // Check if the plugin supports the required format
00189          // else, continue to next plugin
00190          if(strcasecmp(filter->info().service().c_str(), fileFormat.c_str())) {
00191 #ifdef DEBUG
00192 std::cout << "FAILED" << std::endl;
00193 #endif
00194             continue; // to next filter
00195          }
00196       } else if(useFileExt) { // check file extension
00197          // Compare the file extension to the supported extensions of the current plugin
00198 
00199          std::string fileExt = filename.substr(filename.rfind(".")+1);
00200 
00201          // make sure there is indeed an extension to the filename
00202          if(fileExt.size()) {
00203             bool matchFound(false);
00204 #ifdef DEBUG
00205 std::cout << "File Ext : " << fileExt << " .. Checking supported extensions .. " << std::flush;
00206 #endif
00207             std::vector<std::string>::const_iterator iter = filter->getFileExtensions().begin();
00208             std::vector<std::string>::const_iterator iterEnd = filter->getFileExtensions().end();
00209             while(iter!=iterEnd) {
00210 #ifdef DEBUG
00211    std::cout << "\"" << *iter << "\"" << std::flush;
00212    #endif
00213                if(*iter == fileExt) {
00214 #ifdef DEBUG
00215 std::cout << " MATCH!" << std::endl;
00216 #endif
00217                   matchFound = true;
00218                   break; // get out of the while loop
00219                }
00220                ++iter;
00221             }
00222             if(!matchFound) {
00223 #ifdef DEBUG
00224 std::cout << "FAILED" << std::endl;
00225 #endif
00226                continue; // to the next filter
00227             }
00228          } else {
00229 #ifdef DEBUG
00230 std::cout << "FAILED" << std::endl;
00231 #endif
00232             continue; // to next filter
00233          }
00234       }
00235 
00236       // rewind file pointer
00237       rewind(fp);
00238 
00239 
00240       if(!filter->readInfo(volInfo, fp, filename)) {
00241 #ifdef DEBUG
00242 std::cout << "FAILED" << std::endl;
00243 #endif
00244          continue; // to next filter
00245       }
00246 
00247 #ifdef DEBUG
00248 std::cout << "File Info : \nVolume dimensions : " << volInfo.dim() << std::endl;
00249 #endif
00250 
00251       // close file
00252       fclose(fp);
00253 
00254       info = volInfo;
00255 
00256       if(!volInfo.extraInfo()->isDefined("name")) {
00257          // create a name from the filename
00258          std::string name = filename.substr(filename.rfind("/")+1);
00259          if(!name.size())
00260             name = filename;
00261          info.extraInfoRW()->add(name, "name", "Volume name", true);
00262       }
00263 
00264       // success
00265       return(true);
00266 
00267    } // end-for
00268 
00269    // failure
00270    return(false);
00271 }
00272 
00273 
00285 bool vlVolume::read(const std::string & filename, bool useFileExt)
00286 {
00287    vlVolInfo info; // send an invalid info
00288    std::string format; // send an empty string
00289 
00290    // call actual read to read the file
00291    return(read(filename, info, format, useFileExt));
00292 }
00293 
00294 
00304 bool vlVolume::read(const std::string & filename, const std::string & fileFormat)
00305 {
00306    if(!fileFormat.size()) {
00307 #ifdef DEBUG
00308 std::cout << "No file format specified." << std::endl;
00309 #endif
00310       return(false);
00311    }
00312 
00313    vlVolInfo info; // send an invalid info
00314 
00315    // call actual read to read the file
00316    return(read(filename, info, fileFormat, false));
00317 }
00318 
00319 
00332 bool vlVolume::read(const std::string & filename, const vlVolInfo & info, bool useFileExt)
00333 {
00334    // an empty std::string
00335    std::string format;
00336 
00337    // call actual read to read the file
00338    return(read(filename, info, format, useFileExt));
00339 }
00340 
00341 
00342 
00356 bool vlVolume::read(const std::string & filename, const vlVolInfo & info, const std::string & fileFormat, bool useFileExt)
00357 {
00358    // try to open the file
00359    FILE *fp = fopen(filename.c_str(), "r");
00360    if(!fp) {
00361       // error ("unable to open file");
00362 #ifdef DEBUG
00363 std::cout << "Unable to open file " << filename << " for reading." << std::endl;
00364 #endif
00365       return(false);
00366    }
00367 
00368    // make sure vlKernel is initialized
00369    vlKernel::instance();
00370 
00371    // temp variable to store info
00372    vlVolInfo volInfo;
00373 
00374    // get all FileIO plugins
00375    std::vector<vlPlugin*> plugins = vlKernel::trader()->getPluginsForGroup("FileIO");
00376    vlVolFioFilter *filter;
00377    std::vector<vlPlugin*>::iterator itr = plugins.begin();
00378 
00379    // loop over the filters to find the filter that provides IO capability for the given format
00380    for(; itr!=plugins.end(); ++itr) {
00381 #ifdef DEBUG
00382 std::cout << "Trying filter [Name:" << (*itr)->info().name() << std::flush;
00383 #endif
00384 
00385       filter = (vlVolFioFilter*) (*itr);
00386       // make sure the pointer is valid
00387       if(!filter) {
00388 #ifdef DEBUG
00389 std::cout << "FAILED" << std::endl;
00390 #endif
00391          continue;
00392       }
00393 
00394 #ifdef DEBUG
00395 std::cout << ",Format:" << filter->info().service() << "] ... " << std::flush;
00396 #endif
00397 
00398       // Check if file format is specified
00399       if(fileFormat.size()) {
00400          // if file format is specified, then only the plugins of that file format will be used : no
00401          // autodetection.
00402 
00403          // Check if the plugin supports the required format
00404          // else, continue to next plugin
00405          if(filter->info().service() != fileFormat) {
00406 #ifdef DEBUG
00407 std::cout << "FAILED" << std::endl;
00408 #endif
00409             continue; // to next filter
00410          }
00411       } else if(useFileExt) { // check file extension
00412          // Compare the file extension to the supported extensions of the current plugin
00413 
00414          std::string fileExt = filename.substr(filename.rfind(".")+1);
00415 
00416          // make sure there is indeed an extension to the filename
00417          if(fileExt.size()) {
00418             bool matchFound(false);
00419 #ifdef DEBUG
00420 std::cout << "File Ext : " << fileExt << " .. Checking supported extensions .. " << std::flush;
00421 #endif
00422             std::vector<std::string>::const_iterator iter = filter->getFileExtensions().begin();
00423             std::vector<std::string>::const_iterator iterEnd = filter->getFileExtensions().end();
00424             while(iter!=iterEnd) {
00425 #ifdef DEBUG
00426 std::cout << "\"" << *iter << "\"" << std::flush;
00427 #endif
00428                if(*iter == fileExt) {
00429 #ifdef DEBUG
00430 std::cout << " MATCH!" << std::endl;
00431 #endif
00432                   matchFound = true;
00433                   break; // get out of the while loop
00434                }
00435                ++iter;
00436             }
00437             if(!matchFound) {
00438 #ifdef DEBUG
00439 std::cout << "FAILED" << std::endl;
00440 #endif
00441                continue; // to the next filter
00442             }
00443          } else {
00444 #ifdef DEBUG
00445 std::cout << "FAILED" << std::endl;
00446 #endif
00447             continue; // to next filter
00448          }
00449       } //useFileExt
00450 
00451       // rewind file pointer
00452       rewind(fp);
00453 
00454       // Check if given volume information is to be used to open the volume
00455       if(info.isValid()) {
00456 #ifdef DEBUG
00457 std::cout << "[II] Using given info to read the file." << std::endl;
00458 #endif
00459          // use this information to read the file
00460          volInfo = info;
00461 #ifdef DEBUG
00462 std::cout <<filename<<std::endl;
00463 #endif
00464       } else { // read the information from the file using the current plugin
00465          if(!filter->readInfo(volInfo, fp, filename)) {
00466 #ifdef DEBUG
00467 std::cout << "FAILED" << std::endl;
00468 #endif
00469             continue; // to next filter
00470          }
00471       }
00472 
00473       // allocate memory according to the info read
00474       resizeData(volInfo.dim(), volInfo.dataType(), volInfo.units(), dataLayoutName());
00475 
00476       // try to read data using volInfo
00477       if(!filter->readData(volInfo, m_pVolData, fp, filename)) {
00478 #ifdef DEBUG
00479 std::cout << "FAILED" << std::endl;
00480 #endif
00481          continue; // to next filter
00482       } else {
00483 #ifdef DEBUG
00484 std::cout << "OK" << std::endl;
00485 #endif
00486       }
00487 #ifdef DEBUG
00488 std::cout << "File Info : \nVolume dimensions : " << volInfo.dim() << std::endl;
00489 #endif
00490 
00491       // close file
00492       fclose(fp);
00493 
00494       // copy extra info over
00495       ((vlVarList*)(&m_extraInfo))->clear();
00496       *((vlVarList*)(&m_extraInfo)) = *(volInfo.extraInfo());
00497 
00498       if(volInfo.extraInfo()->isDefined("name")) {
00499          // copy the name over
00500          std::string tempName;
00501          volInfo.extraInfo()->get("name", tempName);
00502          m_extraInfo.set("name", tempName);
00503       } else {
00504          // create a name from the filename
00505          std::string name = filename.substr(filename.rfind("/")+1);
00506          if(!name.size())
00507             name = filename;
00508          m_extraInfo.set("name", name);
00509       }
00510 
00511       m_extraInfo.set("origFilename", filename);
00512 
00513       // success
00514       return(true);
00515 
00516    } // end-for
00517 
00518    // failure
00519    return(false);
00520 }
00521 
00522 
00523 template <class DataType>
00524 bool vlVolume::copyDataT(DataType & dummy, vlVolData *fromData, vlVolData *toData, vlPoint3ui fromPos, vlPoint3ui toPos)
00525 {
00526    // copy data same data type from fromData at fromPos to toData at toPos
00527    vlVolIter<DataType> *toItr=new vlVolIter<DataType>(toData);
00528    vlVolIter<DataType> *fromItr = new vlVolIter<DataType>(fromData);
00529    int nx=min((int)fromData->dim().x(),toData->dim().x()-toPos.x());
00530    int ny=min((int)fromData->dim().y(),toData->dim().y()-toPos.y());
00531    int nz=min((int)fromData->dim().z(),toData->dim().z()-toPos.z());
00532 
00533    if(nx<=0||ny<=0||nz<=0) return(false);
00534    for(int slice=0;slice<nz;slice++) {
00535       vlPoint3ui from(fromPos.x(),fromPos.y(),slice+fromPos.z());
00536       vlPoint3ui to(toPos.x(),toPos.y(),slice+toPos.z());
00537 
00538       //#ifdef DEBUG std::cout<<" "<<(int) to.z()<<std::flush;
00539 
00540       vlPoint3ui idx(0,0,0);
00541       for(idx.y(0); idx.y()<ny; idx.y(idx.y()+1)) {
00542          idx.x(0);
00543          fromItr->moveTo(from + idx);
00544          toItr->moveTo(to + idx);
00545          for(; idx.x()<nx; idx.x(idx.x()+1)) {
00546 
00547             toItr->set(fromItr->get());
00548       //if (fromItr->get()>50) std::cout<<fromItr->get()<<std::flush;else 
00549 //std::cout<<"+"<<std::flush;
00550             toItr->nextXYZ();
00551             fromItr->nextXYZ();
00552          }
00553       }
00554    }
00555    return(true);
00556 }
00557 
00558 template <class DataType>
00559 bool vlVolume::cropSliceT(DataType & dummy, vlPoint3ui subVolPos, vlPoint3ui volPos)
00560 {
00561    // copy one slice of same data type from x,y position subVolPos to volPos
00562    vlVolIter<DataType> *iter=new vlVolIter<DataType>(m_pVolData);
00563    vlVolIter<DataType> *slcItr = new vlVolIter<DataType>(m_pSliceData);
00564    vlPoint3ui idx(0,0,0);
00565    for(idx.y(0); idx.y()<dim().y(); idx.y(idx.y()+1)) {
00566       idx.x(0);
00567       slcItr->moveTo(subVolPos + idx);
00568       iter->moveTo(volPos + idx);
00569       for(; idx.x()<dim().x(); idx.x(idx.x()+1)) {
00570          iter->set(slcItr->get());
00571 //if (slcItr->get()>50) std::cout<<slcItr->get()<<std::flush;
00572 //else 
00573 //std::cout<<"/"<<std::flush;
00574          iter->nextXYZ();
00575          slcItr->nextXYZ();
00576       }
00577    }
00578 
00579    return(true);
00580 }
00581 
00582 bool vlVolume::cropREDtoRGBA(vlPoint3ui subVolPos, vlPoint3ui volPos, uint8 threshold)
00583 {
00584    bool RTN_VAL(false);
00585    // copy one slice from x,y position subVolPos to volPos
00586    vlVolIter<vlColorRGBAub> *iter=new vlVolIter<vlColorRGBAub>(m_pVolData);
00587    vlVolIter<vlColorRGBub> *slcItr = new vlVolIter<vlColorRGBub>(m_pSliceData);
00588 
00589    vlPoint3ui idx(0,0,0);
00590    for(idx.y(0); idx.y()<dim().y(); idx.y(idx.y()+1)) {
00591       idx.x(0);
00592       slcItr->moveTo(subVolPos + idx);
00593       iter->moveTo(volPos + idx);
00594       for(; idx.x()<dim().x(); idx.x(idx.x()+1)) {
00595          vlColorRGBAub voxelColor(vlColorRGBAub(slcItr->get().r(),slcItr->get().b(),slcItr->get().b()));
00596          uint8 red=slcItr->get().r();
00597          uint8 green=slcItr->get().g();
00598          uint8 blue=slcItr->get().b();
00599          if(red>=threshold) RTN_VAL=true;
00600          iter->set(voxelColor);
00601          iter->nextXYZ();
00602          slcItr->nextXYZ();
00603       }
00604    }
00605    return(RTN_VAL);
00606 }
00607 
00608 
00609 bool vlVolume::cropRGBtoRGBA(vlPoint3ui subVolPos, vlPoint3ui volPos, uint8 threshold)
00610 {
00611    bool RTN_VAL(false);
00612    // copy one slice from x,y position subVolPos to volPos
00613    vlVolIter<vlColorRGBAub> *iter=new vlVolIter<vlColorRGBAub>(m_pVolData);
00614    vlVolIter<vlColorRGBub> *slcItr = new vlVolIter<vlColorRGBub>(m_pSliceData);
00615 //std::cout<<" ***cropRGBtoRGBA**" <<threshold<<std::flush;
00616    vlPoint3ui idx(0,0,0);
00617    for(idx.y(0); idx.y()<dim().y(); idx.y(idx.y()+1)) {
00618       idx.x(0);
00619       slcItr->moveTo(subVolPos + idx);
00620       iter->moveTo(volPos + idx);
00621       for(; idx.x()<dim().x(); idx.x(idx.x()+1)) {
00622       uint8 red=slcItr->get().r();
00623       uint8 green=slcItr->get().g();
00624             uint8 blue=slcItr->get().b();
00625          vlColorRGBAub voxelColor(red,green,blue);
00626 
00627          if(red>=threshold) RTN_VAL=true;
00628          iter->set(voxelColor);
00629          iter->nextXYZ();
00630          slcItr->nextXYZ();
00631       }
00632    }
00633    return(RTN_VAL);
00634 }
00635 
00636 
00637 bool vlVolume::cropRGBtoRed(vlPoint3ui subVolPos, vlPoint3ui volPos, uint8 threshold) {
00638    // copy one slice from x,y position subVolPos to volPos
00639    vlVolIter<uint8> *iter=new vlVolIter<uint8>(m_pVolData);
00640    vlVolIter<vlColorRGBub> *slcItr = new vlVolIter<vlColorRGBub>(m_pSliceData);
00641 //#ifdef DEBUG std::cout<<" ***cropRGBtoRED***" <<threshold<<std::flush;
00642    vlPoint3ui idx(0,0,0);
00643    bool RTN_VAL;
00644    RTN_VAL=false;
00645    for(idx.y(0); idx.y()<dim().y(); idx.y(idx.y()+1)) {
00646       idx.x(0);
00647       slcItr->moveTo(subVolPos + idx);
00648       iter->moveTo(volPos + idx);
00649       for(; idx.x()<dim().x(); idx.x(idx.x()+1)) {
00650          uint8 red=slcItr->get().r();
00651          if(!RTN_VAL&&red>=threshold) {//#ifdef DEBUG std::cout<<iter->pos()<<" "<<std::flush;
00652    RTN_VAL=true;}
00653          iter->set(red);//if (red>50)#ifdef DEBUG std::cout<<(int)red<<" "<<std::flush;
00654          iter->nextXYZ();
00655          slcItr->nextXYZ();
00656       }
00657    }
00658    return(RTN_VAL);
00659 }
00660 
00686 bool vlVolume::readSubvolumes(const std::string & imgfilepath, const std::string & imgfilelist, const vlVolInfo & imgInfo, const vlVolInfo & info, const unsigned int nsubVols, const unsigned int subVolStart)
00687 {
00688    int readsubvols=0;
00689 
00690    std::string imgname;
00691    int subvol;
00692    subvol=0;
00693    //Get the image file names from disk
00694    FILE *lfp = fopen(imgfilelist.c_str(), "r");
00695    if(!lfp) {
00696       // error ("unable to open file");
00697 #ifdef DEBUG
00698 std::cout << "Unable to open imgfilelist " <<imgfilelist << " for reading." << std::endl;
00699 #endif
00700       return(false);
00701    }
00702 #ifdef DEBUG
00703 std::cout << "OPENED " <<imgfilelist<< std::endl;
00704 #endif
00705    std::vector< std::string > imglist;
00706    char imageFile[100],str[100];
00707    while(1) {
00708 
00709       // read information from the file
00710       fgets(str,100,lfp);
00711       sscanf(str,"%s",imageFile);
00712       imgname=imageFile;
00713       if(subvol>=subVolStart&&subvol<subVolStart+nsubVols) {
00714          imglist.push_back(imgname);readsubvols++;
00715       }
00716       subvol++;
00717       //check if its EOF
00718       if(feof(lfp)) {//subvol>=subVolStart+subvols||
00719 
00720 #ifdef DEBUG
00721 std::cout << "Actual subvols read " << readsubvols << std::endl;
00722 #endif
00723          break;
00724       }
00725 
00726    }
00727 
00728    fclose(lfp);
00729    if(imglist.empty()) return(false);
00730 
00731    else return(readSubvolumes(imgfilepath,imglist,imgInfo,info,readsubvols,subVolStart));
00732 
00733 }
00734 
00735 bool vlVolume::readSubvolumes(const std::string & imgfilepath, std::vector< std::string > imglist, const vlVolInfo & info, const unsigned int nsubVols, const unsigned int subVolStart) {
00736    vlVolInfo imgInfo;
00737 
00738    return(readSubvolumes(imgfilepath,imglist,imgInfo,info,nsubVols,subVolStart));
00739 
00740 }
00741 
00742 
00743 bool vlVolume::readSubvolumes(const std::string & imgfilepath, std::vector< std::string > imglist, const vlVolInfo & imgInfo, const vlVolInfo & info, const unsigned int nsubVols, const unsigned int subVolStart) {
00744    // temp variables to store info
00745    vlVolInfo volInfo;
00746    vlVolInfo subVolInfo;
00747    int subvols;
00748    int readsubvols;
00749    int slices=0;
00750    bool consecutiveSubVols=true;
00751    vlDim dim;
00752    FILE *fp;
00753    vlPoint3ui subVolPos(0,0,0);
00754    vlPoint3ui volPos;
00755    std::string filename;
00756    std::vector< std::string >::const_iterator sitr(imglist.begin());
00757 
00758    if(nsubVols>0) subvols=nsubVols;
00759    else subvols=imglist.size();
00760 
00761    filename=*sitr;
00762 
00763    //make sure vlKernel is initialized
00764    vlKernel::instance();
00765    std::vector<vlPlugin*> plugins = vlKernel::trader()->getPluginsForGroup("FileIO");
00766    vlVolFioFilter *filter;
00767    std::vector<vlPlugin*>::iterator itr = plugins.begin();
00768    std::string fileExt = filename.substr(filename.rfind(".")+1);
00769 
00770    // loop over the filters to find the filter that provides IO capability for the given format
00771    for(; itr!=plugins.end(); ++itr) {
00772 #ifdef DEBUG
00773 std::cout << "Trying filter [Name:" << (*itr)->info().name() << std::flush;
00774 #endif
00775 
00776       filter = (vlVolFioFilter*) (*itr);
00777       // make sure the pointer is valid
00778       if(!filter) {
00779 #ifdef DEBUG
00780 std::cout << "FAILED" << std::endl;
00781 #endif
00782          continue;
00783       }
00784 
00785 #ifdef DEBUG
00786 std::cout << ",Format:" << filter->info().service() << "] ... " << std::flush;
00787 #endif
00788 
00789 
00790       // make sure there is indeed an extension to the filename
00791       if(fileExt.size()) {
00792          bool matchFound(false);
00793 #ifdef DEBUG
00794 std::cout << "File Ext : " << fileExt << " .. Checking supported extensions .. " << std::flush;
00795 #endif
00796          std::vector<std::string>::const_iterator iter = filter->getFileExtensions().begin();
00797          std::vector<std::string>::const_iterator iterEnd = filter->getFileExtensions().end();
00798          while(iter!=iterEnd) {
00799 #ifdef DEBUG
00800 std::cout << "\"" << *iter << "\"" << std::flush;
00801 #endif
00802             if(*iter == fileExt) {
00803 #ifdef DEBUG
00804 std::cout << " MATCH!" << std::endl;
00805 #endif
00806                matchFound = true;
00807                break; // get out of the while loop
00808             }
00809             ++iter;
00810          }
00811          if(!matchFound) {
00812 #ifdef DEBUG
00813 std::cout << "FAILED" << std::endl;
00814 #endif
00815             continue; // to the next filter
00816          }
00817       } else {
00818 #ifdef DEBUG
00819 std::cout << "FAILED" << std::endl;
00820 #endif
00821          continue; // to next filter
00822       }
00823 
00824       // Check if given volume information is to be used to open the volume
00825       if(info.isValid()) {
00826          volInfo = info;
00827          //m_origFilePos=info.origFilePos();
00828       }
00829       //Called to crop all subvolumes at info.origFilePos
00830       if(imgInfo.isValid()) {
00831 #ifdef DEBUG
00832 std::cout << "Equal Dimension SubVolumes "  << std::flush;
00833 #endif
00834          subVolPos=vlPoint3ui(imgInfo.origFilePos().x(),imgInfo.origFilePos().y(),0);
00835          slices=subvols*imgInfo.dim().z();
00836          if(!info.isValid()) volInfo = imgInfo;
00837          m_origFilePos=imgInfo.origFilePos();
00838          dim=vlDim(volInfo.dim().x(),volInfo.dim().y(),slices);
00839          subVolInfo=imgInfo;
00840       } else {
00841          //Determine dimensions and origFilePos of new volume to fit subvolumes
00842          int minx,miny,minz;
00843          int maxx=0;
00844          int maxy=0;
00845          int maxz=0;
00846          readsubvols=0;
00847 //#ifdef DEBUG
00848 std::cout<<"Using subvolume files for info ... "<< std::endl;
00849 //#endif
00850          for(; sitr!=imglist.end(); ++sitr) {
00851             filename = *sitr;
00852             if(readsubvols>=subVolStart+subvols&&subvols>0) break;
00853             if(readsubvols>=subVolStart) {
00854                filename=imgfilepath+filename;
00855 #ifdef DEBUG
00856          std::cout<<filename<<"  "<< std::endl;
00857 #endif
00858                fp = fopen(filename.c_str(), "r");
00859 
00860                if(!filter->readInfo(subVolInfo, fp, filename)) {
00861 #ifdef DEBUG
00862 std::cout << "FAILED" << std::endl;
00863 #endif
00864                   return(false);
00865                }
00866                if(readsubvols==subVolStart) {
00867                   minx=subVolInfo.origFilePos().x();
00868                   miny=subVolInfo.origFilePos().y();
00869                   minz=subVolInfo.origFilePos().z();
00870                   // #ifdef DEBUG std::cout<<"minx "<<minx<<" miny "<<miny<<" minz "<<minz<<std::endl; #endif
00871 
00872                }
00873                int ux=subVolInfo.origFilePos().x()+subVolInfo.dim().x();
00874                int uy=subVolInfo.origFilePos().y()+subVolInfo.dim().y();
00875                int uz=subVolInfo.origFilePos().z()+subVolInfo.dim().z();
00876 std::cout<<filename<<subVolInfo.dim()<<"  x:"<<subVolInfo.origFilePos().x()<<"-"<<ux<<"  y:"<<subVolInfo.origFilePos().y()<<"-"<<uy<<"  z:"<<subVolInfo.origFilePos().z()<<"-"<<uz<<std::endl;
00877                if(ux>maxx) maxx=ux;
00878                if(uy>maxy) maxy=uy;
00879                if(uz>maxz) maxz=uz;
00880                if(subVolInfo.origFilePos().x()<minx) minx=subVolInfo.origFilePos().x();
00881                if(subVolInfo.origFilePos().y()<miny) miny=subVolInfo.origFilePos().y();
00882                if(subVolInfo.origFilePos().z()<minz) minz=subVolInfo.origFilePos().z();
00883                if(subVolInfo.origFilePos().z()>0) consecutiveSubVols=false;
00884                slices+=subVolInfo.dim().z();
00885             }
00886             readsubvols++;
00887          }
00888          m_origFilePos=vlPoint3ui(minx,miny,minz);
00889          if(consecutiveSubVols) dim=vlDim(maxx-minx,maxy-miny,slices);
00890          else
00891             dim=vlDim(maxx-minx,maxy-miny,maxz-minz);
00892 //#ifdef DEBUG
00893    std::cout<<"maxy-miny "<<maxy<<"-"<<miny <<" dim "<<dim<<" m_origFilePos  "<<m_origFilePos<<std::endl;
00894 //#endif
00895       }// Subvolume info not given
00896       vlDim odim(volInfo.origFileDim());
00897       m_origFileDim=odim;
00898       resizeData(dim, volInfo.dataType(), volInfo.units(), dataLayoutName());
00899       int px=0;
00900       int py=0;
00901       int pz=0;
00902       readsubvols=0;
00903       //std::vector< std::string >::const_iterator svitr(imglist.begin());
00904       sitr=imglist.begin();
00905       for(; sitr!=imglist.end(); ++sitr) {
00906          filename = *sitr;
00907          //#ifdef DEBUG std::cout<<filename<<"  "<< std::endl; #endif
00908          if(readsubvols>=subVolStart+subvols) break;
00909          if(readsubvols>=subVolStart) {
00910             filename=imgfilepath+filename;
00911 
00912             if(imgInfo.isValid()) {
00913                fp = fopen(filename.c_str(), "r");
00914                if(!fp) {
00915 #ifdef DEBUG
00916 std::cout << "Unable to open file " << filename << " for reading." << std::endl;
00917 #endif
00918                   return(false);
00919                }
00920                // All subvolume positions are equal so place at start of volume
00921                volPos=vlPoint3ui(0, 0, pz);
00922                pz+=subVolInfo.dim().z();
00923             } else {
00924                if(!filter->readInfo(subVolInfo, fp, filename)) {
00925 #ifdef DEBUG
00926 std::cout << "FAILED" << std::endl;
00927 #endif
00928                   return(false);
00929                }
00930                subVolInfo.setDataType(volInfo.dataType());
00931                //Shift precropped subvolumes to correct relative postion in volume
00932                px=subVolInfo.origFilePos().x()-m_origFilePos.x();
00933                py=subVolInfo.origFilePos().y()-m_origFilePos.y();
00934                if(consecutiveSubVols) {
00935                   volPos=vlPoint3ui(px,py,pz);
00936                   pz+=subVolInfo.dim().z()-1;
00937                } else {
00938                   pz=subVolInfo.origFilePos().z()-m_origFilePos.z();
00939                   volPos=vlPoint3ui(px,py,pz);
00940                }
00941                if(px<0||py<0||pz<0) return(false); //Shouldn't happen
00942             }
00943 //#ifdef DEBUG std::cout << "px, py, pz "<<px<< "  " <<py<< "  " <<pz<< std::flush; #endif
00944 
00945             m_pSliceData = createData(subVolInfo.dim(), volInfo.dataType(), volInfo.units(), dataLayoutName());
00946             fp = fopen(filename.c_str(), "r");
00947             if(!filter->readData(subVolInfo, m_pSliceData, fp, filename)) {
00948 #ifdef DEBUG
00949 std::cout << "FAILED" << std::endl;
00950 #endif
00951                return(false);
00952             }
00953             bool retValue(false);
00954 #ifdef DEBUG
00955 std::cout<<" copyDataT From: "<<subVolPos<<" to: "<<volPos<< std::endl;
00956 #endif
00957             callFunctionOnDataType(m_pVolData->dataType(), retValue, copyDataT, m_pSliceData, m_pVolData, subVolPos, volPos);
00958 
00959             // close file
00960             fclose(fp);
00961             delete m_pSliceData;
00962          }// Subvolumes being used
00963          readsubvols++;
00964       } // Subvolume list loop
00965       // success
00966       return(true);
00967    } // end-for  // failure
00968    return(false);
00969 }
00970 
00971 
00985 std::queue< std::string >  vlVolume::readImages(const std::string & imgfilepath, const std::string & imgfilelist, const vlVolInfo & info, const unsigned int start) {
00986    vlVolInfo imgInfo;
00987    return(readImages(imgfilepath,imgfilelist,imgInfo,info,start));
00988 }
00989 
00990 
00991 
00992 std::queue< std::string >  vlVolume::readImages(const std::string & imgfilepath, const std::string & imgfilelist,
00993      const vlVolInfo & imgInfo, const vlVolInfo & info, const unsigned int start)
00994 {
00995    std::queue< std::string > verifiedImages;
00996    // temp variables to store info
00997    int slices;
00998    int readslices=0;
00999    //vlDim dim(info.dim());
01000    slices=info.dim().z();
01001 
01002    std::string imgname;
01003    int slice;
01004    slice=0;
01005    //Get the image file names from disk
01006    FILE *lfp = fopen(imgfilelist.c_str(), "r");
01007    if(!lfp) {
01008       // error ("unable to open file");
01009 #ifdef DEBUG
01010 std::cout << "Unable to open imgfilelist " <<imgfilelist << " for reading." << std::endl;
01011 #endif
01012       return(verifiedImages);
01013    }
01014 
01015    std::queue<std::string> imglist;
01016    char imageFile[100],str[100];
01017    while(1) {
01018 
01019       // read information from the file
01020       fgets(str,100,lfp);
01021       sscanf(str,"%s",imageFile);
01022       imgname=imageFile;
01023       if(slice>=start&&slice<start+slices) {
01024          imglist.push(imgname);
01025    readslices++;
01026       }
01027       slice++;
01028       //check if its EOF
01029       if(feof(lfp)) {
01030          //#ifdef DEBUG std::cout << "Actual slices read " << readslices << std::endl; #endif
01031          break;
01032       }
01033 //dim.z(readslices);
01034       info.dim().z(readslices);
01035    }
01036 
01037    fclose(lfp);
01038    if(imglist.empty()) return(verifiedImages);
01039    else return(readImages(imgfilepath,imglist,imgInfo,info,start));
01040 }
01041 
01042 std::queue< std::string > vlVolume::readImages(const std::string & imgfilepath, std::queue< std::string > imglist, const vlVolInfo & imgInfo, const vlVolInfo & info, const unsigned int start=0){
01043    std::queue<std::string> verifiedImages;
01044 uint8 threshold=50;
01045 
01046    std::string filename;
01047    vlVolInfo volInfo;
01048    vlVolInfo slcInfo;
01049    // Check if given volume information is to be used to open the volume
01050    if(info.isValid()) {
01051       volInfo = info;
01052    } else {
01053       return(verifiedImages);
01054    }
01055    int slices;
01056    slices=info.dim().z();//imglist.size();
01057    //std::cout<< imglist.size()<<"imglist READIMAGES info "<<info.dim().z()<<std::endl;
01058 //info.dim().z();
01059    std::string imgname;
01060    int slice;
01061    slice=0;
01062 
01063    filename=imgfilepath+imglist.front();//imgname;
01064    // make sure vlKernel is initialized
01065    vlKernel::instance();
01066    std::vector<vlPlugin*> plugins = vlKernel::trader()->getPluginsForGroup("FileIO");
01067    vlVolFioFilter *filter;
01068    std::vector<vlPlugin*>::iterator itr = plugins.begin();
01069    std::string fileExt = filename.substr(filename.rfind(".")+1);//Use last filename read from imgfilelist
01070 
01071    // loop over the filters to find the filter that provides IO capability for the given format
01072    for(; itr!=plugins.end(); ++itr) {
01073 #ifdef DEBUG
01074 std::cout << "Trying filter [Name:" << (*itr)->info().name() << std::flush;
01075 #endif
01076 
01077       filter = (vlVolFioFilter*) (*itr);
01078       // make sure the pointer is valid
01079       if(!filter) {
01080 #ifdef DEBUG
01081 std::cout << "FAILED" << std::endl;
01082 #endif
01083          continue;
01084       }
01085 
01086 #ifdef DEBUG
01087 std::cout << ",Format:" << filter->info().service() << "] ... " << std::flush;
01088 #endif
01089 
01090 
01091       // make sure there is indeed an extension to the filename
01092       if(fileExt.size()) {
01093          bool matchFound(false);
01094 #ifdef DEBUG
01095 std::cout << "File Ext : " << fileExt << " .. Checking supported extensions .. " << std::flush;
01096 #endif
01097          std::vector<std::string>::const_iterator iter = filter->getFileExtensions().begin();
01098          std::vector<std::string>::const_iterator iterEnd = filter->getFileExtensions().end();
01099          while(iter!=iterEnd) {
01100 #ifdef DEBUG
01101 std::cout << "\"" << *iter << "\"" << std::flush;
01102 #endif
01103             if(*iter == fileExt) {
01104 #ifdef DEBUG
01105 std::cout << " MATCH!" << std::endl;
01106 #endif
01107                matchFound = true;
01108                break; // get out of the while loop
01109             }
01110             ++iter;
01111          }
01112          if(!matchFound) {
01113 #ifdef DEBUG
01114 std::cout << "FAILED" << std::endl;
01115 #endif
01116             continue; // to the next filter
01117          }
01118       } else {
01119 #ifdef DEBUG
01120 std::cout << "FAILED" << std::endl;
01121 #endif
01122          continue; // to next filter
01123       }
01124       // try to open the file
01125       FILE *fp = fopen(filename.c_str(), "r");//.c_str()
01126       if(!fp) {
01127          // error ("unable to open file");
01128 #ifdef DEBUG
01129 std::cout << "Unable to open file " << filename << " for reading." << std::endl;
01130 #endif
01131          return(verifiedImages);
01132       }
01133       // Slice files may have a different data type and bytes per voxel than the volume we're
01134       // creating. Read info if it's not given.
01135       // Check if given volume information is to be used to open the volume
01136       if(imgInfo.isValid()) {
01137          // use this information to read the file
01138          slcInfo = imgInfo;
01139 #ifdef DEBUG
01140 std::cout << "[II] Using given info to read the image files. "  <<filename<< std::endl;
01141 #endif
01142       } else { // read the information from the file using the current plugin
01143 #ifdef DEBUG
01144 std::cout<<" USING "<<filename<<" for image info "<< std::endl;
01145 #endif
01146 
01147          if(!filter->readInfo(slcInfo, fp, filename)) {
01148 #ifdef DEBUG
01149 std::cout << "FAILED" << std::endl;
01150 #endif
01151             return(verifiedImages);
01152          }
01153       }
01154 
01155       vlDim dim(volInfo.dim().x(),volInfo.dim().y(),slices);
01156       // allocate memory according to the info read
01157       resizeData(dim, volInfo.dataType(), volInfo.units(), dataLayoutName());
01158       //#ifdef DEBUG std::cout << "dim"<<dim.x()<<", "<<dim.y()<<", "<<dim.z()<<std::endl; #endif
01159 m_origFilePos=volInfo.origFilePos();
01160 #ifdef CDEBUG
01161      std::cout <<"SET m_origFilePos "<<m_origFilePos<<std::endl;
01162 #endif
01163 
01164 
01165       int validSlices=0;
01166       for(slice=0;slice<slices;slice++) {
01167 
01168          // try to open the file
01169          imgname=imglist.front();
01170 
01171          filename = imgfilepath+imgname;//imglist.front();
01172 #ifdef CDEBUG
01173 std::cout << filename << " " << std::flush;
01174 #endif
01175 imglist.pop();
01176          fp = fopen(filename.c_str(), "r");
01177          if(!fp) {
01178 #ifdef DEBUG
01179 std::cout << "Unable to open file " << filename << " for reading." << std::endl;
01180 #endif
01181             //return(verifiedImages);
01182          }else {
01183 
01184          m_pSliceData = createData(slcInfo.dim(), slcInfo.dataType(), slcInfo.units(), dataLayoutName());
01185          if(!filter->readData(slcInfo, m_pSliceData, fp, filename)) {
01186 #ifdef DEBUG
01187 std::cout << "FAILED" << std::endl;
01188 #endif
01189             continue; // to next filter
01190          }
01191          fclose(fp);
01192          bool valid(false);
01193          vlPoint3ui subVolPos(m_origFilePos.x(), m_origFilePos.y(), 0);
01194          vlPoint3ui volPos(0, 0, validSlices);
01195          if(volInfo.dataType()==slcInfo.dataType()) {
01196 
01197             callFunctionOnDataType(slcInfo.dataType(), valid, cropSliceT, subVolPos, volPos);
01198             if(valid) {
01199                verifiedImages.push(imgname);
01200                validSlices++;
01201         // #ifdef DEBUG std::cout << "  " << imgname << std::flush; #endif
01202             }else {
01203 #ifdef DEBUG
01204 std::cout << imgname<< " EMPTY " << std::endl;
01205 #endif
01206       }
01207          } else {
01208            if(volInfo.bytesPerVoxel()==4&&slcInfo.bytesPerVoxel()==1) {
01209                valid=cropREDtoRGBA(subVolPos, volPos, threshold);
01210                if(valid) {
01211                  verifiedImages.push(imgname);
01212                  validSlices++;
01213            //#ifdef DEBUG std::cout << "  " << imgname << std::flush; #endif
01214                } else {
01215 #ifdef DEBUG
01216 std::cout << imgname<< " EMPTY " << std::endl;
01217 #endif
01218 }
01219          }else {
01220          if(volInfo.bytesPerVoxel()==4&&slcInfo.bytesPerVoxel()==3) {
01221                valid=cropRGBtoRGBA(subVolPos, volPos, threshold);
01222                if(valid) {
01223                  verifiedImages.push(imgname);
01224                  validSlices++;
01225            //#ifdef DEBUG std::cout << "  " << imgname << std::flush; #endif
01226                }else {
01227 #ifdef DEBUG
01228 std::cout << imgname<< " EMPTY " << std::endl;
01229 #endif
01230          }
01231             } else {
01232                if(volInfo.bytesPerVoxel()==1&&slcInfo.bytesPerVoxel()==3) {
01233                   valid=cropRGBtoRed(subVolPos, volPos, threshold);
01234                   if(valid) {
01235                      verifiedImages.push(imgname);
01236                      validSlices++;
01237          //#ifdef DEBUG std::cout << "  " << imgname << std::flush; #endif
01238                   }else {
01239 #ifdef DEBUG
01240 std::cout << imgname<< " EMPTY " << std::endl;
01241 #endif
01242          }
01243                }
01244             }
01245       }//Not RED to RGBA
01246          }//Different data types
01247          delete m_pSliceData;
01248 }
01249       } //slices loop
01250       const vlPoint3ui cropStart(m_origFilePos);
01251       vlDim newDim(dim.x(),dim.y(),validSlices);
01252 //#ifdef DEBUG std::cout << "FOUND " <<validSlices<< " valid slices" <<std::endl; #endif
01253 
01254 if(validSlices<slices) {
01255       //#ifdef DEBUG std::cout << "FOUND BAD SLICE: " <<validSlices<< " valid slices" <<std::endl; #endif
01256 
01257          resize(newDim, true, cropStart);
01258       }
01259       // success
01260       return(verifiedImages);
01261 
01262    } // end-for  // failure
01263    std::cout <<"READIMAGES m_origFilePos "<<m_origFilePos<<std::endl;
01264 
01265    return(verifiedImages);
01266 
01267 }
01268 
01270 
01279 bool vlVolume::write(const std::string & filename) const
01280 {
01281    // pass an empty string for format
01282    std::string format;
01283    return(write(filename, format, false));
01284 }
01285 
01286 
01297 bool vlVolume::write(const std::string & filename, const std::string & fileFormat,
01298      const bool appendExtension) const
01299 {
01300    if(!isValid()) {
01301 #ifdef DEBUG
01302 std::cout << "The current volume is not valid. Cannot proceed with write." << std::endl;
01303 #endif
01304       return(false);
01305    }
01306    // get all FileIO plugins
01307    std::vector<vlPlugin*> plugins = vlKernel::trader()->getPluginsForGroup("FileIO");
01308    vlVolFioFilter *filter;
01309    unsigned int i;
01310    std::vector<vlPlugin*>::iterator itr = plugins.begin();
01311    FILE *fp;
01312    std::string finalFileName(filename);
01313 
01314    // loop over the filters to find the filter that provides IO capability for the given format
01315    for(; itr!=plugins.end(); ++itr) {
01316 #ifdef DEBUG
01317 std::cout << "Trying filter [Name:" << (*itr)->info().name() << std::flush;
01318 #endif
01319 
01320       filter = (vlVolFioFilter*) (*itr);
01321       // make sure the pointer is valid
01322       if(!filter) {
01323 #ifdef DEBUG
01324 std::cout << "FAILED" << std::endl;
01325 #endif
01326          continue;
01327       }
01328 
01329 #ifdef DEBUG
01330 std::cout << ",Format:" << filter->info().service() << "] ... " << std::flush;
01331 #endif
01332 
01333       // Check if file format is specified
01334       if(fileFormat.size()) {
01335          // if file format is specified, then only that plugins of that file format will be used : no
01336          // autodetection.
01337 
01338          // Check if the plugin supports the required format
01339          // else, continue to next plugin
01340          if(strcasecmp(filter->info().service().c_str(), fileFormat.c_str())) {
01341 #ifdef DEBUG
01342 std::cout << "FAILED" << std::endl;
01343 #endif
01344             continue; // to next filter
01345          }
01346       } else { // try to guess file format from file extension
01347 
01348          std::string fileExt = filename.substr(filename.rfind(".")+1);
01349 
01350          // make sure there is indeed an extension to the filename
01351          if(fileExt.size()) {
01352             bool matchFound(false);
01353 #ifdef DEBUG
01354 std::cout << "File Ext : " << fileExt << " .. Checking supported extensions .. " << std::flush;
01355 #endif
01356             std::vector<std::string>::const_iterator iter = filter->getFileExtensions().begin();
01357             std::vector<std::string>::const_iterator iterEnd = filter->getFileExtensions().end();
01358             while(iter!=iterEnd) {
01359 #ifdef DEBUG
01360 std::cout << "\"" << *iter << "\"" << std::flush;
01361 #endif
01362                if(*iter == fileExt) {
01363 #ifdef DEBUG
01364 std::cout << " MATCH!" << std::endl;
01365 #endif
01366                   matchFound = true;
01367                   break; // get out of the while loop
01368                }
01369                ++iter;
01370             }
01371             if(!matchFound) {
01372 #ifdef DEBUG
01373 std::cout << "FAILED" << std::endl;
01374 #endif
01375                continue; // to the next filter
01376             }
01377          } else {
01378 #ifdef DEBUG
01379 std::cout << "FAILED" << std::endl;
01380 #endif
01381             continue; // to next filter
01382          }
01383       }
01384 
01385       // got the filter.. now get out!
01386 #ifdef DEBUG
01387 std::cout << "OK" << std::endl;
01388 #endif
01389 #ifdef DEBUG
01390 std::cout << "Trying to write using this plugin ... " << std::flush;
01391 #endif
01392 
01393       // Append extension if needed
01394       if(appendExtension) {
01395          std::string extension;
01396          // make sure there is a default extension
01397          if(filter->getFileExtensions().size()) {
01398             extension = filter->getFileExtensions().front();
01399             // make sure the default extension is not null
01400             if(extension.size()) {
01401                // make sure the current filename doesn't have the same extension already
01402                if(filename.substr(filename.rfind(".")+1) != extension) {
01403                   finalFileName=filename+"."+extension;
01404                }
01405             }
01406          }
01407       }
01408 
01409       // try to open the file
01410       fp = fopen(finalFileName.c_str(), "w");
01411       if(!fp) {
01412 #ifdef DEBUG
01413 std::cout << "\nUnable to open file " << finalFileName << " for writing." << std::endl;
01414 #endif
01415          return(false);
01416       }
01417 
01418       // Now try to write using the plugin
01419       // If this plugin is not successful, delete the file and try next plugin.
01420 
01421       vlVolInfo *volInfo = getVolInfo();
01422       vlPoint3ui filePos=volInfo->origFilePos();
01423       // create a name from the filename
01424       // Write the header
01425       if(!filter->writeInfo(*volInfo, fp, finalFileName)) {
01426 #ifdef DEBUG
01427 std::cout << "FAILED" << "\nERROR writing header. Checking for another plugin." << std::endl;
01428 #endif
01429 
01430          fclose(fp);
01431          continue;
01432       }
01433 
01434       // Write the data
01435       if(!filter->writeData(*volInfo, m_pVolData, fp, finalFileName)) {
01436 #ifdef DEBUG
01437 std::cout << "FAILED" << "\nERROR writing data. Checking for another plugin." << std::endl;
01438 #endif
01439          fclose(fp);
01440          continue;
01441       }
01442 
01443       // success :)
01444       fclose(fp);
01445 #ifdef DEBUG
01446 std::cout << "\nWrote file : " << finalFileName << std::endl;
01447 #endif
01448       return(true);
01449    }
01450 
01451    // failure
01452    return(false);
01453 }
01454 
01461 bool vlVolume::resize(vlDim const & newDim, const bool keep, const vlPoint3ui cropStart)
01462 {
01463    if(!m_pVolData)
01464       return(false);
01465    else
01466 
01467       if(keep) {
01468       bool retValue;
01469       m_pSliceData =  createData(newDim, m_pVolData->dataType(), m_pVolData->units(), dataLayoutName());
01470       callFunctionOnDataType(m_pVolData->dataType(), retValue, copyDataT, m_pVolData, m_pSliceData,cropStart);
01471       resizeData(newDim, m_pVolData->dataType(), m_pVolData->units(), dataLayoutName());
01472       callFunctionOnDataType(m_pVolData->dataType(), retValue, copyDataT, m_pSliceData, m_pVolData);
01473       delete m_pSliceData;
01474       m_origFilePos=cropStart;
01475    } else
01476 
01477       return(resizeData(newDim, m_pVolData->dataType(), m_pVolData->units(), dataLayoutName()));
01478 
01479 }
01480 
01488 bool vlVolume::resizeData(const vlDim & newDim, const vlDataType dataType,
01489      const vlUnit & units, const std::string & layout)
01490 {
01491    // delete any old data that exists
01492    if(m_pVolData)
01493       delete m_pVolData;
01494    if(m_layoutPlugin) {
01495       if(m_layoutPlugin->info().service() == layout) {
01496          vlVolData *data = m_layoutPlugin->getLayout(dataType, newDim, units);
01497 
01498          if(!data) {
01499 #ifdef DEBUG
01500 std::cout << "ERROR : vlVolume() - Plugin returns null for layout. [" << layout << "]" << std::endl;
01501 #endif
01502             return(false);
01503          }
01504 
01505          m_pVolData = data;
01506          return(true);
01507       } else {
01508          // layout has changed
01510          delete m_layoutPlugin;
01511       }
01512    }
01513 
01514    vlVolDataLayoutPlugin *plugin =
01515         dynamic_cast<vlVolDataLayoutPlugin *> (vlKernel::trader()->getPlugin("VolDataLayout", layout));
01516 
01517    if(!plugin) {
01518 #ifdef DEBUG
01519 std::cout << "ERROR : vlVolume() - No plugin found for requested layout [" << layout << "]" << std::endl;
01520 #endif
01521       return(false);
01522    }
01523 
01524    vlVolData *data = plugin->getLayout(dataType, newDim, units);
01525 
01526    if(!data) {
01527 #ifdef DEBUG
01528 std::cout << "ERROR : vlVolume() - Plugin returns null for layout. [" << layout << "]" << std::endl;
01529 #endif
01530       return(false);
01531    }
01532 
01533    m_pVolData = data;
01534 
01535    // store the plugin pointer for quick access
01536    m_layoutPlugin = plugin;
01537 //#ifdef DEBUG std::cout << "newDim "<<newDim<<" dataType "<<dataType<< std::endl; #endif
01538 
01539    return(true);
01540 }
01541 
01542 
01550 vlVolData* vlVolume::createData(const vlDim & newDim, const vlDataType dataType,
01551      const vlUnit & units, const std::string & layout)
01552 {
01553    vlVolData *data;
01554 
01555    if(m_layoutPlugin) {
01556       if(m_layoutPlugin->info().service() == layout) {
01557          data = m_layoutPlugin->getLayout(dataType, newDim, units);
01558 
01559          if(!data) {
01560 #ifdef DEBUG
01561 std::cout << "ERROR : vlVolume() - Plugin returns null for layout. [" << layout << "]" << std::endl;
01562 #endif
01563             return(false);
01564          }
01565 
01566          return(data);
01567       } else {
01568          // layout has changed
01570          delete m_layoutPlugin;
01571       }
01572    }
01573 
01574    vlVolDataLayoutPlugin *plugin =
01575         dynamic_cast<vlVolDataLayoutPlugin *> (vlKernel::trader()->getPlugin("VolDataLayout", layout));
01576 
01577    if(!plugin) {
01578 #ifdef DEBUG
01579 std::cout << "ERROR : vlVolume() - No plugin found for requested layout [" << layout << "]" << std::endl;
01580 #endif
01581       return(false);
01582    }
01583 
01584    data = plugin->getLayout(dataType, newDim, units);
01585 
01586    if(!data) {
01587 #ifdef DEBUG
01588 std::cout << "ERROR : vlVolume() - Plugin returns null for layout. [" << layout << "]" << std::endl;
01589 #endif
01590       return(0L);
01591    }
01592 
01593    // store the plugin pointer for quick access
01594    //m_layoutPlugin = plugin;
01595 
01596    return(data);
01597 }
01598 
01599 bool vlVolume::clear(const uint8 data)
01600 {
01601    if(m_pVolData)
01602       return(m_pVolData->clear(data));
01603    else
01604       return(false);
01605 }
01606 
01607 
01608 vlDim vlVolume::dim() const
01609 {
01610    if(m_pVolData)
01611       return(m_pVolData->dim());
01612    else
01613       return(vlDim(0,0,0));
01614 }
01615 
01616 
01617 vlStep vlVolume::stepping() const
01618 {
01619    if(m_pVolData)
01620       return(m_pVolData->stepping());
01621    else
01622       return(vlStep(0,0,0));
01623 }
01624 
01625 
01626 vlUnit vlVolume::units() const
01627 {
01628    if(m_pVolData)
01629       return(m_pVolData->units());
01630    else
01631       return(vlUnit(0,0,0));
01632 }
01633 
01634 
01635 uint16 vlVolume::bitsPerVoxel() const
01636 {
01637    if(m_pVolData)
01638       return(m_pVolData->bitsPerVoxel());
01639    else
01640       return(0);
01641 }
01642 
01643 
01644 uint16 vlVolume::bytesPerVoxel() const
01645 {
01646    if(m_pVolData)
01647       return(m_pVolData->bytesPerVoxel());
01648    else
01649       return(0);
01650 }
01651 
01652 
01653 uint64 vlVolume::voxelCount() const
01654 {
01655    if(m_pVolData)
01656       return(m_pVolData->voxelCount());
01657    else
01658       return(0);
01659 }
01660 
01661 
01662 vlDataType vlVolume::dataType() const
01663 {
01664    if(m_pVolData)
01665       return(m_pVolData->dataType());
01666    else
01667       return(UnknownDataType);
01668 }
01669 
01671 const char * vlVolume::typeInfo() const
01672 {
01673    if(m_pVolData)
01674       return(m_pVolData->typeInfo());
01675    else
01676       return(0L);
01677 }
01678 
01679 
01680 vlLayoutType vlVolume::dataLayout() const
01681 {
01682    if(m_pVolData)
01683       return(m_pVolData->layout());
01684    else
01685       return(vlLayout::Linear);
01686 };
01687 
01688 
01689 std::string vlVolume::dataLayoutName() const
01690 {
01691    if(m_layoutPlugin)
01692       return(m_layoutPlugin->info().service());
01693    else
01694       return("Unknown");
01695 }
01696 
01697 
01698 bool vlVolume::isValid() const
01699 {
01700    if(m_pVolData) {
01701       if(!m_pVolData->isValid())
01702          return(false);
01703       else
01704          return(true);
01705    } else
01706       return(false);
01707 }
01708 
01710 bool vlVolume::isDirty() const
01711 {
01712    if(m_pVolData)
01713       return m_pVolData->isDirty();
01714    else
01715       return(false);
01716 }
01717 
01719 void vlVolume::setDirty(bool dirty)
01720 {
01721    if(m_pVolData)
01722       m_pVolData->setDirty(dirty);
01723 }
01724 
01725 
01727 const vlVarList * vlVolume::extraInfo() const
01728 {
01729    return(&m_extraInfo);
01730 }
01731 
01733 vlVarList * vlVolume::extraInfoRW()
01734 {
01735    return(&m_extraInfo);
01736 }
01737 
01738 
01739 void * vlVolume::getVoxelVoidPtr(const vlPoint3ui & position) const
01740 {
01741    if(m_pVolData)
01742       return(m_pVolData->getVoxelVoidPtr(position));
01743    else
01744       return 0L;
01745 }
01746 
01747 
01748 vlVolInfo * vlVolume::getVolInfo() const
01749 {
01750    vlVolInfo *info = new vlVolInfo();
01751 
01752    info->setDim(dim());
01753    info->setStepping(stepping());
01754    info->setDataType(dataType());
01755    info->setUnits(units());
01756    info->setBitsPerVoxel(bitsPerVoxel());
01757    info->setBytesPerVoxel(bytesPerVoxel());
01758    info->setOrigFilePos(m_origFilePos);
01759    info->setOrigFileDim(m_origFileDim);
01760    info->setExtraInfo(&m_extraInfo);
01761 
01762    return(info);
01763 }
01764 
01765 vlVolDataLayoutPlugin * vlVolume::dataLayoutPlugin() const
01766 {
01767    return(m_layoutPlugin);
01768 }
01769 
01770 
01771 

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