Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

SparseImage3D.hxx

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _SparseImage3D_hxx
00021 #define _SparseImage3D_hxx
00022 
00023 #include<ImLib3D/SparseImage3D.hpp>
00024 #include<ImLib3D/XMLTools.hpp>
00025 
00026 template<class Im3DValue>
00027 void 
00028 SparseImage3D<Im3DValue>::Write(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node) const
00029 {
00030     node=CreateWriteNode(file,parentNode,node);
00031     // **** Optionally add unset image properties ***
00032     // set image type (Image3Df, Field3D etc....) 
00033     if(!file->HasAttribute(node,"Type")){node->set_attribute("Type",GetTypeName());}
00034 
00035     // store sparseStructure
00036     xmlpp::Element *sNode=node->add_child("sparseStructure");
00037     if(!shareSparseStructureFile){sparseStructure->Write(file,sNode);}
00038     else
00039     {
00040         if(sparseStructure->Name()=="")
00041         {
00042             ThrowError("SparseImage3D<Im3DValue>::Write sparseStructure file is shared, but has sparseStructure has no name");
00043         }
00044         sNode->set_attribute("sparseStructureFile",sparseStructure->Name());
00045     }
00046     // store Data (underlying image) information
00047     xmlpp::Element *dataN=node->add_child("ImageData");
00048     file->AddBinaryNode(dataN,sparseStructure->ValSize()*sizeof(value_type));
00049     file->template BinaryWrite<Im3DValue,Im3DValue *>(imageData,imageDataEnd);
00050     // write parents data (image size and properties)
00051     Image3D::Write(file,NULL,node);
00052 }
00053 template<class Im3DValue>
00054 void 
00055 SparseImage3D<Im3DValue>::Read(ImLib3DFile *file,xmlpp::Element *parentNode,xmlpp::Element *node)
00056 {
00057     node=CreateReadNode(file,parentNode,node);
00058     Image3D::Read(file,NULL,node);
00059     //type checking
00060     string type=file->GetAttribute(node,"Type");
00061     if(type!=GetTypeName()){ThrowError("SparseImage3D<Im3DValue>::Read: Expected type:\"%s\" found type:\"%s\"",GetTypeName(),type);}
00062     // store sparseStructure
00063     xmlpp::Element *sNode=file->GetChild(node,"sparseStructure");
00064     string sparseStructureFile=GetAttribute(sNode,"sparseStructureFile");
00065     // if sparseStructureFile file is not shared just get it from this node
00066     if(sparseStructureFile==""){shareSparseStructureFile=false;sparseStructure=SparseStructure3D::Create(file,sNode);}
00067     else
00068     {
00069         shareSparseStructureFile=true;
00070         // check if already in mem
00071         sparseStructure=SparseStructure3D::Find(sparseStructureFile);
00072         if(!sparseStructure)
00073         {// not yet loaded, must create
00074             sparseStructure=SparseStructure3D::Create(sparseStructureFile);
00075         }
00076         // in both cases tell sparseStructure that somedy else is using it
00077         sparseStructure->Reference(this);
00078     }
00079     //image data
00080     xmlpp::Element *dataN=file->GetChild(node,"ImageData");
00081     file->ReadBinaryNode(dataN);
00082     Allocate();
00083     file->template BinaryRead<Im3DValue,Im3DValue *>(imageData,imageDataEnd);
00084     if(!file->CheckBinaryReadComplete())
00085     {ThrowError("XMLBinaryFile::BinaryRead bad read size (maybe wrong image type?) ");}
00086 }
00087 
00088 template<class Im3DValue>
00089 void 
00090 SparseImage3D<Im3DValue>::Allocate()
00091 {
00092     if(imageData){delete [] imageData;}
00093     imageData=new Im3DValue[sparseStructure->ValSize()];
00094     imageDataEnd=imageData+sparseStructure->ValSize();
00095 }
00096 
00097 // *******************************************************
00098 template<class Im3DValue>
00099 void 
00100 SparseImage3D<Im3DValue>::Resize(const Size3D &newSize)
00101 {
00102     if(Size()!=newSize && Size()!=Size3D(0,0,0)){ThrowError("SparseImage3D<Im3DValue>::Resize not allowed");}
00103     Image3D::Resize(newSize);
00104 }
00105 
00106 
00107 // *******************************************************
00108 template<class Im3DValue>
00109 SparseImage3D<Im3DValue>& 
00110 SparseImage3D<Im3DValue>::operator=(const SparseImage3D & other)
00111 {
00112     if (this == &other){return *this;}
00113     sparseStructure->Dereference(this);
00114     SetSparseStructure(other.sparseStructure);
00115     Image3D::operator=(other);
00116     Allocate();
00117     memcpy((void *)imageData,(void *)other.imageData,sizeof(Im3DValue)*sparseStructure->ValSize());
00118     return *this;
00119 }
00120 
00121 
00122 // *******************************************************
00123 template<class Im3DValue>
00124 bool 
00125 SparseImage3D<Im3DValue>::operator==(const SparseImage3D & other) const
00126 {
00127     if(other.Size()!=Size()){return false;}
00128     if(!equal(begin(),end(),other.begin())){return false;}  
00129     if(!(properties==other.properties)){return false;}
00130     
00131     return true;
00132 }
00133 
00134 
00135 
00136 // *******************************************************
00137 template<class Im3DValue>
00138 SparseImage3D<Im3DValue>::~SparseImage3D()
00139 {
00140     if(sparseStructure){sparseStructure->Dereference(this);}
00141     else
00142     {printf("deleting invalidated SparseImage3D\n");}
00143     delete [] imageData;
00144 }
00145 
00146 // *******************************************************
00147 //  SparseImage3D<Im3DValue>::SparseImage3D(int _width, int _height, int _depth) : 
00148 //      Image3D(_width,_height,_depth)
00149 //  {
00150 //      Resize(Size3D(_width,_height,_depth));
00151 //  }
00152 // *******************************************************
00153 template<class Im3DValue>
00154 SparseImage3D<Im3DValue>::SparseImage3D():
00155     SparseImage3DBase(),
00156     imageData(NULL),imageDataEnd(NULL)
00157 {
00158     InitZero();
00159 }
00160 
00161 // *******************************************************
00162 template<class Im3DValue>
00163 SparseImage3D<Im3DValue>::SparseImage3D(const SparseStructure3D *_sparseStructure):
00164     SparseImage3DBase(_sparseStructure),
00165     imageData(NULL),imageDataEnd(NULL)
00166 {
00167     InitZero();
00168     Allocate();
00169 }
00170 
00171 // *******************************************************
00172 template<class Im3DValue>
00173 SparseImage3D<Im3DValue>::SparseImage3D(const SparseImage3D & other) :
00174     SparseImage3DBase(other),
00175     imageData(NULL),imageDataEnd(NULL)
00176 {
00177     InitZero();
00178     Allocate();
00179     memcpy((void *)imageData,(void *)other.imageData,sizeof(Im3DValue)*sparseStructure->ValSize());
00180 }
00181 
00182 
00183 #endif // _SparseImage3D_hxx

Generated on Fri Jun 17 13:36:07 2005 for ImLib3D by  doxygen 1.4.2