core/vil/file_formats/vil_openjpeg_pyramid_image_resource.cxx
Go to the documentation of this file.
00001 #include "vil_openjpeg_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_openjpeg_pyramid_image_resource::
00020 vil_openjpeg_pyramid_image_resource(vil_image_resource_sptr const &openjpeg)
00021 : openjpeg_sptr_(openjpeg)
00022 {
00023   ptr_ = 0;
00024   if (!openjpeg_sptr_)
00025     return;
00026   ptr_ = dynamic_cast<vil_openjpeg_image*>(openjpeg_sptr_.ptr());
00027 }
00028 
00029 unsigned vil_openjpeg_pyramid_image_resource::nplanes() const
00030 {
00031   unsigned ret = 0;
00032   if (ptr_)
00033     ret =  ptr_->nplanes();
00034   return ret;
00035 }
00036 
00037 //: The number of pixels in each row.
00038 // Dimensions:  Planes x ni x nj.
00039 // This method refers to the base (max resolution) image
00040 unsigned vil_openjpeg_pyramid_image_resource::ni() const
00041 {
00042   unsigned ret = 0;
00043   if (ptr_)
00044     ret = ptr_->ni();
00045   return ret;
00046 }
00047 
00048 //: The number of pixels in each column.
00049 // Dimensions:  Planes x ni x nj.
00050 // This method refers to the base (max resolution) image
00051 unsigned vil_openjpeg_pyramid_image_resource::nj() const
00052 {
00053   unsigned ret = 0;
00054   if (ptr_)
00055     ret =  ptr_->nj();
00056   return ret;
00057 }
00058 
00059 //: Pixel Format.
00060 vil_pixel_format vil_openjpeg_pyramid_image_resource::pixel_format() const
00061 {
00062   if (ptr_)
00063     return  ptr_->pixel_format();
00064   return VIL_PIXEL_FORMAT_UNKNOWN;
00065 }
00066 
00067 //: Return a string describing the file format.
00068 // Only file images have a format, others return 0
00069 char const* vil_openjpeg_pyramid_image_resource::file_format() const
00070 {
00071   return "openjpeg_pyramid";
00072 }
00073 
00074 
00075   // === Methods particular to pyramid resource ===
00076 
00077 //: Number of pyramid levels.
00078 unsigned vil_openjpeg_pyramid_image_resource::nlevels() const
00079 {
00080   if (!ptr_)
00081     return 0;
00082   return ptr_->nreductions() + 1;
00083 }
00084 
00085 //: Get a partial view from the image from a specified pyramid level
00086 vil_image_view_base_sptr
00087 vil_openjpeg_pyramid_image_resource::get_copy_view(unsigned i0, unsigned ni,
00088                                                    unsigned j0, unsigned nj,
00089                                                    unsigned level) const
00090 {
00091   if (!ptr_||!(ptr_->is_valid()))
00092      return 0;
00093   if (level >= this->nlevels())
00094     level = this->nlevels() - 1;
00095   return ptr_->get_copy_view_reduced(i0, ni, j0, nj, level);
00096 }
00097 
00098 //: Get a partial view from the image in the pyramid closest to scale.
00099 // The origin and size parameters are in the coordinate system of the base image.
00100 // The scale factor is with respect to the base image (base scale = 1.0).
00101 vil_image_view_base_sptr
00102 vil_openjpeg_pyramid_image_resource::get_copy_view(unsigned i0, unsigned ni,
00103                                                    unsigned j0, unsigned nj,
00104                                                    const float scale,
00105                                                    float& actual_scale) const
00106 {
00107   if (scale >= 1.0f)
00108   {
00109     actual_scale = 1.0f;
00110     return this->get_copy_view(i0, ni, j0, nj, 0);
00111   }
00112   float f_lev = -vcl_log(scale) / vcl_log(2.0f);
00113   unsigned level = static_cast<unsigned>(f_lev);
00114   if (level >= this->nlevels())
00115     level = this->nlevels()-1;
00116   actual_scale = scale_at_level(level);
00117   return this->get_copy_view(i0, ni, j0, nj, level);
00118 }
00119 
00120 //: Get an image resource from the pyramid at the specified level
00121 vil_image_resource_sptr
00122 vil_openjpeg_pyramid_image_resource::get_resource(const unsigned level) const
00123 {
00124   if (level == 0)
00125     return openjpeg_sptr_;
00126   return 0;
00127 }
00128 
00129 //: for debug purposes
00130 void vil_openjpeg_pyramid_image_resource::print(const unsigned level)
00131 {
00132 }