core/vil/file_formats/vil_j2k_nitf2_pyramid_image_resource.cxx
Go to the documentation of this file.
00001 #include "vil_j2k_nitf2_pyramid_image_resource.h"
00002 //:
00003 // \file
00004 // Do not remove the following notice
00005 // Approved for public Release, distribution unlimited
00006 // DISTAR Case 14074
00007 
00008 #include <vcl_cmath.h>
00009 
00010 // By definition, each level is a factor of 2 reduced in scale
00011 static float scale_at_level(unsigned level)
00012 {
00013   if (level == 0)
00014     return 1.0f;
00015   float s = vcl_pow(2.0f, -static_cast<float>(level));
00016   return s;
00017 }
00018 
00019 vil_j2k_nitf2_pyramid_image_resource::
00020 vil_j2k_nitf2_pyramid_image_resource(vil_image_resource_sptr const& nitf2): nitf2_sptr_(nitf2)
00021 {
00022   ptr_ = 0;
00023   if (!nitf2_sptr_)
00024     return;
00025   vcl_string fmt = nitf2_sptr_->file_format();
00026   if (fmt=="nitf21")
00027     ptr_=static_cast<vil_nitf2_image*>(nitf2_sptr_.ptr());
00028 }
00029 
00030 unsigned vil_j2k_nitf2_pyramid_image_resource::nplanes()const
00031 {
00032   unsigned ret = 0;
00033   if (ptr_)
00034     ret =  ptr_->nplanes();
00035   return ret;
00036 }
00037 
00038 //: The number of pixels in each row.
00039 // Dimensions:  Planes x ni x nj.
00040 // This method refers to the base (max resolution) image
00041 unsigned vil_j2k_nitf2_pyramid_image_resource::ni() const
00042 {
00043   unsigned ret = 0;
00044   if (ptr_)
00045     ret = ptr_->ni();
00046   return ret;
00047 }
00048 
00049 //: The number of pixels in each column.
00050 // Dimensions:  Planes x ni x nj.
00051 // This method refers to the base (max resolution) image
00052 unsigned vil_j2k_nitf2_pyramid_image_resource::nj() const
00053 {
00054   unsigned ret = 0;
00055   if (ptr_)
00056     ret =  ptr_->nj();
00057   return ret;
00058 }
00059 
00060 //: Pixel Format.
00061 vil_pixel_format vil_j2k_nitf2_pyramid_image_resource::pixel_format() const
00062 {
00063   if (ptr_)
00064     return  ptr_->pixel_format();
00065   return VIL_PIXEL_FORMAT_UNKNOWN;
00066 }
00067 
00068 //: Return a string describing the file format.
00069 // Only file images have a format, others return 0
00070 char const* vil_j2k_nitf2_pyramid_image_resource::file_format() const
00071 {
00072   return "j2k_nitf2";
00073 }
00074 
00075 
00076   // === Methods particular to pyramid resource ===
00077 
00078   //: Number of pyramid levels.
00079   //  Defined to assign 1000 pixels to the largest
00080   //  dimension of the least resolution level
00081 unsigned vil_j2k_nitf2_pyramid_image_resource::nlevels() const
00082 {
00083   if (!ptr_)
00084     return 0;
00085   unsigned max_dim = this->ni(), nj = this->nj();
00086   if (nj>max_dim)
00087     max_dim = nj;
00088   if (max_dim<1000)
00089     return 1;
00090   // Assume top level of the pyramid has maximum dimension of 1K pixels
00091   double scale = max_dim/1000.0;
00092   if (scale<=1.0)
00093     return 1;
00094   double lscale = vcl_log(scale);
00095   unsigned nlev = static_cast<unsigned>(lscale/vcl_log(2.0));
00096   return nlev;
00097 }
00098 
00099   //: Get a partial view from the image from a specified pyramid level
00100 vil_image_view_base_sptr
00101 vil_j2k_nitf2_pyramid_image_resource::get_copy_view(unsigned i0, unsigned n_i,
00102                                                     unsigned j0, unsigned n_j,
00103                                                     unsigned level) const
00104 {
00105   if (!ptr_||!(ptr_->is_jpeg_2000_compressed()))
00106      return 0;
00107   if (level>this->nlevels())
00108     level = this->nlevels()-1;
00109   double s = scale_at_level(level);
00110   double factor = static_cast<unsigned>(1/s);
00111   return ptr_->get_copy_view_decimated_j2k(i0, n_i, j0, n_j, factor, factor);
00112 }
00113 
00114   //: Get a partial view from the image in the pyramid closest to scale.
00115   // The origin and size parameters are in the coordinate system of the base image.
00116   // The scale factor is with respect to the base image (base scale = 1.0).
00117 vil_image_view_base_sptr
00118 vil_j2k_nitf2_pyramid_image_resource::get_copy_view(unsigned i0, unsigned n_i,
00119                                                     unsigned j0, unsigned n_j,
00120                                                     const float scale,
00121                                                     float& actual_scale) const
00122 {
00123   if (scale>=1.0f)
00124   {
00125     actual_scale = 1.0f;
00126     return this->get_copy_view(i0, n_i, j0, n_j, 0);
00127   }
00128   float f_lev = -vcl_log(scale)/vcl_log(2.0f);
00129   unsigned level = static_cast<unsigned>(f_lev);
00130   if (level>this->nlevels())
00131     level = this->nlevels()-1;
00132   actual_scale = scale_at_level(level);
00133   return this->get_copy_view(i0, n_i, j0, n_j, level);
00134 }
00135 
00136 //: Get an image resource from the pyramid at the specified level
00137 vil_image_resource_sptr
00138 vil_j2k_nitf2_pyramid_image_resource::get_resource(const unsigned level) const
00139 {
00140   if (level==0)
00141     return nitf2_sptr_;
00142   return 0;
00143 }
00144 
00145 //: for debug purposes
00146 void vil_j2k_nitf2_pyramid_image_resource::print(const unsigned level)
00147 {
00148 }
00149