contrib/mul/vil3d/vil3d_save.cxx
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_save.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 //
00008 
00009 
00010 #include "vil3d_save.h"
00011 
00012 #include <vcl_cstring.h>
00013 #include <vcl_iostream.h>
00014 #include <vxl_config.h> // for vxl_byte
00015 
00016 #include <vil3d/vil3d_new.h>
00017 #include <vil3d/vil3d_copy.h>
00018 #include <vil/vil_pixel_format.h>
00019 #include <vil3d/vil3d_image_resource.h>
00020 #include <vil3d/vil3d_image_view.h>
00021 
00022 
00023 //: Send vil3d_image to disk.
00024 bool vil3d_save(const vil3d_image_view_base &im, char const* filename, char const* file_format)
00025 {
00026 
00027   vil3d_image_resource_sptr out =
00028     vil3d_new_image_resource(filename, im.ni(), im.nj(), im.nk(),
00029                              im.nplanes() * vil_pixel_format_num_components(im.pixel_format()),
00030                              im.pixel_format(), file_format);
00031   if (!out) {
00032     vcl_cerr << __FILE__ ": (vil3d_save) Cannot save to type [" << file_format << "]\n";
00033     return false;
00034   }
00035 
00036   // Use smart copy constructor to convert multi-component images
00037   // into multi-plane ones.
00038   switch (vil_pixel_format_component_format(im.pixel_format()))
00039   {
00040   case VIL_PIXEL_FORMAT_BYTE:
00041     return out->put_view(vil3d_image_view<vxl_byte>(im),0,0,0);
00042   case VIL_PIXEL_FORMAT_UINT_16:
00043     return out->put_view(vil3d_image_view<vxl_uint_16>(im),0,0,0);
00044   case VIL_PIXEL_FORMAT_INT_16:
00045     return out->put_view(vil3d_image_view<vxl_int_16>(im),0,0,0);
00046   case VIL_PIXEL_FORMAT_UINT_32:
00047     return out->put_view(vil3d_image_view<vxl_uint_32>(im),0,0,0);
00048   case VIL_PIXEL_FORMAT_INT_32:
00049     return out->put_view(vil3d_image_view<vxl_int_32>(im),0,0,0);
00050   case VIL_PIXEL_FORMAT_FLOAT:
00051     return out->put_view(vil3d_image_view<float>(im),0,0,0);
00052   case VIL_PIXEL_FORMAT_BOOL:
00053     return out->put_view(vil3d_image_view<bool>(im),0,0,0);
00054   case VIL_PIXEL_FORMAT_SBYTE:
00055     return out->put_view(vil3d_image_view<vxl_sbyte>(im),0,0,0);
00056   case VIL_PIXEL_FORMAT_DOUBLE:
00057     return out->put_view(vil3d_image_view<double>(im),0,0,0);
00058   default:
00059     // In case any one has an odd pixel format that actually works with this file_format.
00060     return out->put_view(im, 0, 0, 0);
00061   }
00062 }
00063 
00064 
00065 char const *vil3d_save_guess_file_format(char const* filename)
00066 {
00067   char const *file_format;
00068 
00069   // find last "."
00070   char const *dot = vcl_strrchr(filename, '.');
00071   if (!dot) {
00072     // filename doesn't end in ".anything"
00073     vcl_cerr << __FILE__ ": assuming gipl format for \'" << filename << "\'\n";
00074     file_format = "gipl";
00075   }
00076   else {
00077     // translate common extensions into known file formats.
00078     if (false) { }
00079 #define macro(ext, fmt) else if (!vcl_strcmp(dot, "." #ext)) file_format = #fmt
00080     macro(dcm, dicom);
00081     macro(gpl, gipl);
00082 #undef macro
00083     else
00084       file_format=dot+1;
00085   }
00086 
00087   return file_format;
00088 }
00089 
00090 //: save to file, deducing format from filename.
00091 bool vil3d_save(const vil3d_image_view_base & i, char const* filename)
00092 {
00093   return vil3d_save(i, filename, vil3d_save_guess_file_format(filename));
00094 }
00095 
00096 //: Send vil3d_image to disk.
00097 bool vil3d_save_image_resource(const vil3d_image_resource_sptr &ir, char const* filename,
00098                              char const* file_format)
00099 {
00100   vil3d_image_resource_sptr out = vil3d_new_image_resource(filename, ir->ni(), ir->nj(), ir->nk(),
00101                                                            ir->nplanes(),
00102                                                            ir->pixel_format(), file_format);
00103   if (!out) {
00104     vcl_cerr << __FILE__ ": (vil3d_save) Cannot save type [" << file_format << "] to ["<<filename<<"]\n";
00105     return false;
00106   }
00107   return vil3d_copy_deep(ir, out);
00108 }
00109 
00110 //: save to file, deducing format from filename.
00111 bool vil3d_save_image_resource(const vil3d_image_resource_sptr &ir, char const* filename)
00112 {
00113   return vil3d_save_image_resource(ir, filename, vil3d_save_guess_file_format(filename));
00114 }
00115 
00116 //: Send a vil3d_image_view to disk, deducing format from filename
00117 //  Utility function, allowing definition of voxel widths in header info.
00118 // \relatesalso vil3d_image_view
00119 bool vil3d_save(const vil3d_image_view_base & im, 
00120                 float voxel_width_i,
00121                 float voxel_width_j,
00122                 float voxel_width_k,
00123                 char const* filename)
00124 {
00125   char const* file_format=vil3d_save_guess_file_format(filename);
00126   vil3d_image_resource_sptr out =
00127     vil3d_new_image_resource(filename, im.ni(), im.nj(), im.nk(),
00128                              im.nplanes() * vil_pixel_format_num_components(im.pixel_format()),
00129                              im.pixel_format(), file_format);
00130   if (!out) {
00131     vcl_cerr << __FILE__ ": (vil3d_save) Cannot save to type [" << file_format << "]\n";
00132     return false;
00133   }
00134 
00135   if (!out->set_voxel_size(voxel_width_i,voxel_width_j,voxel_width_k))
00136   {
00137     vcl_cerr << __FILE__ ": (vil3d_save) Cannot save voxel sizes."<<vcl_endl;
00138     return false;
00139   }
00140 
00141   // Use smart copy constructor to convert multi-component images
00142   // into multi-plane ones.
00143   switch (vil_pixel_format_component_format(im.pixel_format()))
00144   {
00145   case VIL_PIXEL_FORMAT_BYTE:
00146     return out->put_view(vil3d_image_view<vxl_byte>(im),0,0,0);
00147   case VIL_PIXEL_FORMAT_UINT_16:
00148     return out->put_view(vil3d_image_view<vxl_uint_16>(im),0,0,0);
00149   case VIL_PIXEL_FORMAT_INT_16:
00150     return out->put_view(vil3d_image_view<vxl_int_16>(im),0,0,0);
00151   case VIL_PIXEL_FORMAT_UINT_32:
00152     return out->put_view(vil3d_image_view<vxl_uint_32>(im),0,0,0);
00153   case VIL_PIXEL_FORMAT_INT_32:
00154     return out->put_view(vil3d_image_view<vxl_int_32>(im),0,0,0);
00155   case VIL_PIXEL_FORMAT_FLOAT:
00156     return out->put_view(vil3d_image_view<float>(im),0,0,0);
00157   case VIL_PIXEL_FORMAT_BOOL:
00158     return out->put_view(vil3d_image_view<bool>(im),0,0,0);
00159   case VIL_PIXEL_FORMAT_SBYTE:
00160     return out->put_view(vil3d_image_view<vxl_sbyte>(im),0,0,0);
00161   case VIL_PIXEL_FORMAT_DOUBLE:
00162     return out->put_view(vil3d_image_view<double>(im),0,0,0);
00163   default:
00164     // In case any one has an odd pixel format that actually works with this file_format.
00165     return out->put_view(im, 0, 0, 0);
00166   }
00167 }
00168