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