00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00032
00033 if(!file->HasAttribute(node,"Type")){node->set_attribute("Type",GetTypeName());}
00034
00035
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
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
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
00060 string type=file->GetAttribute(node,"Type");
00061 if(type!=GetTypeName()){ThrowError("SparseImage3D<Im3DValue>::Read: Expected type:\"%s\" found type:\"%s\"",GetTypeName(),type);}
00062
00063 xmlpp::Element *sNode=file->GetChild(node,"sparseStructure");
00064 string sparseStructureFile=GetAttribute(sNode,"sparseStructureFile");
00065
00066 if(sparseStructureFile==""){shareSparseStructureFile=false;sparseStructure=SparseStructure3D::Create(file,sNode);}
00067 else
00068 {
00069 shareSparseStructureFile=true;
00070
00071 sparseStructure=SparseStructure3D::Find(sparseStructureFile);
00072 if(!sparseStructure)
00073 {
00074 sparseStructure=SparseStructure3D::Create(sparseStructureFile);
00075 }
00076
00077 sparseStructure->Reference(this);
00078 }
00079
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
00148
00149
00150
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