contrib/mul/vimt/vimt_save.cxx
Go to the documentation of this file.
00001 // This is mul/vimt/vimt_save.cxx
00002 #include "vimt_save.h"
00003 //:
00004 // \file
00005 // \author Ian Scott, Kevin de Souza
00006 // \note Adapted from vimt3d_save
00007 
00008 #include <vil/vil_image_resource.h>
00009 #include <vil/vil_new.h>
00010 #include <vil/vil_save.h>
00011 #include <vimt/vimt_transform_2d.h>
00012 #include <vimt/vimt_image_2d.h>
00013 #include <vimt/vimt_vil_v2i.h>
00014 #include <mbl/mbl_log.h>
00015 
00016 static mbl_logger& logger()
00017 {
00018   static mbl_logger l("mul.vimt.save");
00019   return l;
00020 }
00021 
00022 
00023 //: Create a transform from the properties of image resource.
00024 // The transform will be from world co-ordinates in metres to image co-ordinates (or mm if requested).
00025 void vimt_save_transform(vil_image_resource_sptr &ir,
00026                          const vimt_transform_2d& trans,
00027                          bool use_millimetres /*=false*/)
00028 {
00029   if (dynamic_cast<vimt_vil_v2i_image *>(ir.ptr()))
00030   {
00031     vgl_vector_2d<double> pix_per_mm = trans.delta(vgl_point_2d<double>(0,0),
00032                                                    vgl_vector_2d<double>(1.0, 1.0));
00033 
00034     vimt_transform_2d tr;
00035     //const double units_scaling = use_millimetres ? 1000.0 : 1.0;
00036     tr.set_zoom_only(1000.0*pix_per_mm.x(), 1000.0*pix_per_mm.y());
00037 
00038     static_cast<vimt_vil_v2i_image &>(*ir).set_world2im(tr);
00039   }
00040   else
00041   {
00042     vimt_transform_2d i2w=trans.inverse();
00043     vgl_vector_2d<double> dp = i2w.delta(vgl_point_2d<double> (0,0),
00044                                          vgl_vector_2d<double> (1.0, 1.0));
00045     MBL_LOG(WARN, logger(), "vimt_save_transform(): function set_pixel_size()"
00046             " is not yet defined for vil_image_resource base class,"
00047             " only for vimt_vil_v2i_image derived class.");
00048     //if (!ir->set_pixel_size(float(dp.x()),float(dp.y())))
00049     MBL_LOG(WARN, logger(), "vimt_save_transform(): Unable to include pixel sizes:"
00050             <<dp.x()<<','<<dp.y());
00051   }
00052 }
00053 
00054 
00055 bool vimt_save(const vcl_string& path,
00056                const vimt_image_2d& image,
00057                bool use_millimetres /*=false*/)
00058 {
00059   const vimt_image_2d & iv = image;
00060   const vil_image_view_base & ib = iv.image_base();
00061 
00062   vil_image_resource_sptr ir = vil_new_image_resource(
00063     path.c_str(), ib.ni(), ib.nj(), ib.nplanes(), ib.pixel_format(),
00064     vil_save_guess_file_format(path.c_str()));
00065 
00066   if (!ir)
00067     return false;
00068 
00069   ir->put_view(ib);
00070 
00071   vimt_save_transform(ir, image.world2im(), use_millimetres);
00072   return true;
00073 }