00001
00012
00013
00014
00015 #include "analyze_data_io.h"
00016 #include "../global/global_definitions.h"
00017
00018 #include "one_byte_image_pixel_dataset.h"
00019 #include "two_byte_image_pixel_dataset.h"
00020 #include "four_byte_image_pixel_dataset.h"
00021 #include "one_byte_volume_pixel_dataset.h"
00022 #include "two_byte_volume_pixel_dataset.h"
00023 #include "four_byte_volume_pixel_dataset.h"
00024
00025 #include <typeinfo>
00026 #include <itkAnalyzeImageIO.h>
00027
00028 namespace imedgine
00029 {
00030 AnalyzeDataIO::AnalyzeDataIO()
00031 {
00032 }
00033
00034
00035
00036 AnalyzeDataIO::~AnalyzeDataIO()
00037 {
00038 }
00039
00040
00041
00042 std::pair<dataset_pointer_type, dataset_type>
00043 AnalyzeDataIO::loadDataset(std::string filename, dataset_key_type dataset_key)
00044 {
00045 itk::AnalyzeImageIO::Pointer dataset_info = itk::AnalyzeImageIO::New();
00046 dataset_info->SetFileName(filename.c_str());
00047 dataset_info->ReadImageInformation();
00048
00049 #ifdef DEBUG
00050 std::cout << "Num bytes per pixel: " << dataset_info->GetPixelStride() << std::endl
00051 << "Num dimensions: " << dataset_info->GetNumberOfDimensions() << std::endl;
00052 #endif
00053
00054
00055
00056 pixel_dataset_pointer_type pixel_dataset;
00057 dataset_type type = UNDEF_DATASET;
00058
00059
00060 switch (dataset_info->GetPixelStride())
00061 {
00062 case 1 :
00063 {
00064 switch (dataset_info->GetNumberOfDimensions())
00065 {
00066 case 2 :
00067 {
00068 pixel_dataset =
00069 this->read<OneByteImagePixelDataset>(filename, dataset_key);
00070 type = ONE_BYTE_IMAGE_PIXEL_DATASET;
00071 break;
00072 }
00073 case 3 :
00074 {
00075 pixel_dataset =
00076 this->read<OneByteVolumePixelDataset>(filename, dataset_key);
00077 type = ONE_BYTE_VOLUME_PIXEL_DATASET;
00078 break;
00079 }
00080 }
00081 break;
00082 }
00083 case 2 :
00084 {
00085 switch (dataset_info->GetNumberOfDimensions())
00086 {
00087 case 2 :
00088 {
00089 pixel_dataset =
00090 this->read<TwoByteImagePixelDataset>(filename, dataset_key);
00091 break;
00092 type = TWO_BYTE_IMAGE_PIXEL_DATASET;
00093 }
00094 case 3 :
00095 {
00096 pixel_dataset =
00097 this->read<TwoByteVolumePixelDataset>(filename, dataset_key);
00098 type = TWO_BYTE_VOLUME_PIXEL_DATASET;
00099 break;
00100 }
00101 }
00102 break;
00103 }
00104 case 4 :
00105 {
00106 switch (dataset_info->GetNumberOfDimensions())
00107 {
00108 case 2 :
00109 {
00110 pixel_dataset =
00111 this->read<FourByteImagePixelDataset>(filename, dataset_key);
00112 type = FOUR_BYTE_IMAGE_PIXEL_DATASET;
00113 break;
00114 }
00115 case 3 :
00116 {
00117 pixel_dataset =
00118 this->read<FourByteVolumePixelDataset>(filename, dataset_key);
00119 type = FOUR_BYTE_VOLUME_PIXEL_DATASET;
00120 break;
00121 }
00122 }
00123 break;
00124 }
00125 }
00126
00127 for (unsigned char dim_index = 0;
00128 dim_index < dataset_info->GetNumberOfDimensions(); ++dim_index)
00129 {
00130
00131 pixel_dataset->setFocusPointAt(
00132 dim_index, static_cast<index_type>(
00133 static_cast<double>(pixel_dataset->getSize()[dim_index]) / 2.0));
00134
00135 #ifdef DEBUG
00136 std::cout << "Dataset size dimension " << dim_index << " : " <<
00137 static_cast<index_type>(
00138 static_cast<double>(pixel_dataset->getSize()[X_DIMENSION]) / 2.0) << std::endl;
00139 #endif
00140 }
00141
00142 return(std::make_pair(
00143 dataset_pointer_type(pixel_dataset), type));
00144 }
00145
00146
00147
00148 void AnalyzeDataIO::writeDataset(dataset_pointer_type dataset, std::string filename)
00149 throw(InvalidCastException)
00150 {
00151 pixel_dataset_pointer_type pixel_dataset(
00152 dataset, boost::detail::dynamic_cast_tag());
00153
00154 if (pixel_dataset.get() == 0)
00155 {
00156 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "dataset_pointer_type",
00157 "pixel_dataset_pointer_type"));
00158 }
00159
00160 switch (pixel_dataset->getNumBytesPerPixel())
00161 {
00162 case 1 :
00163 {
00164 switch (pixel_dataset->getNumDimensions())
00165 {
00166 case 2 :
00167 {
00168 one_byte_image_pixel_dataset_pointer_type one_byte_image(
00169 pixel_dataset, boost::detail::dynamic_cast_tag());
00170 if (one_byte_image.get() == 0)
00171 {
00172 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00173 "one_byte_image_pixel_dataset_pointer_type"));
00174 }
00175 this->write(one_byte_image, filename);
00176 break;
00177 }
00178 case 3 :
00179 {
00180 one_byte_volume_pixel_dataset_pointer_type one_byte_volume(
00181 pixel_dataset, boost::detail::dynamic_cast_tag());
00182 if (one_byte_volume.get() == 0)
00183 {
00184 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00185 "one_byte_volume_pixel_dataset_pointer_type"));
00186 }
00187 this->write(one_byte_volume, filename);
00188 break;
00189 }
00190 }
00191 break;
00192 }
00193 case 2 :
00194 {
00195 switch (pixel_dataset->getNumDimensions())
00196 {
00197 case 2 :
00198 {
00199 two_byte_image_pixel_dataset_pointer_type two_byte_image(
00200 pixel_dataset, boost::detail::dynamic_cast_tag());
00201 if (two_byte_image.get() == 0)
00202 {
00203 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00204 "two_byte_image_pixel_dataset_pointer_type"));
00205 }
00206 this->write(two_byte_image, filename);
00207 break;
00208 }
00209 case 3 :
00210 {
00211 two_byte_volume_pixel_dataset_pointer_type two_byte_volume(
00212 pixel_dataset, boost::detail::dynamic_cast_tag());
00213 if (two_byte_volume.get() == 0)
00214 {
00215 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00216 "two_byte_volume_pixel_dataset_pointer_type"));
00217 }
00218 this->write(two_byte_volume, filename);
00219 break;
00220 }
00221 }
00222 break;
00223 }
00224 case 4 :
00225 {
00226 switch (pixel_dataset->getNumDimensions())
00227 {
00228 case 2 :
00229 {
00230 four_byte_image_pixel_dataset_pointer_type four_byte_image(
00231 pixel_dataset, boost::detail::dynamic_cast_tag());
00232 if (four_byte_image.get() == 0)
00233 {
00234 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00235 "four_byte_image_pixel_dataset_pointer_type"));
00236 }
00237 this->write(four_byte_image, filename);
00238 break;
00239 }
00240 case 3 :
00241 {
00242 four_byte_volume_pixel_dataset_pointer_type four_byte_volume(
00243 pixel_dataset, boost::detail::dynamic_cast_tag());
00244 if (four_byte_volume.get() == 0)
00245 {
00246 throw(InvalidCastException("AnalyzeDataIO::writeDataset", "pixel_dataset_pointer_type",
00247 "four_byte_volume_pixel_dataset_pointer_type"));
00248 }
00249 this->write(four_byte_volume, filename);
00250 break;
00251 }
00252 }
00253 break;
00254 }
00255 }
00256
00257
00258 }
00259 }