contrib/gel/vifa/vifa_image_histogram.cxx
Go to the documentation of this file.
00001 // This is gel/vifa/vifa_image_histogram.cxx
00002 #include "vifa_image_histogram.h"
00003 //:
00004 // \file
00005 
00006 #include <vil/vil_image_view.h>
00007 
00008 vifa_image_histogram::
00009 vifa_image_histogram(vil_image_view_base_sptr  image,
00010                      double                    percentage)
00011 {
00012   // Compute max. # of pixel intensities
00013   form_ = image->pixel_format();
00014   byte_depth_ = vil_pixel_format_sizeof_components(form_) *
00015                 vil_pixel_format_num_components(form_);
00016 
00017   // Initialize the underlying histogram, based on the image pixel format
00018   init();
00019 
00020   // Fill the histogram
00021   fill_histogram(image, percentage);
00022 }
00023 
00024 void vifa_image_histogram::
00025 init(void)
00026 {
00027   num = 1 << (byte_depth_ * 8);
00028 
00029   // Delete these because they have already been allocated by base constructor
00030   delete [] vals;
00031   delete [] counts;
00032 
00033   vals = new float [num];
00034   counts = new float [num];
00035 
00036   delta = 1.0;
00037 
00038   if (vals != NULL && counts != NULL)
00039   {
00040     register float*  pval = vals;
00041     register float*  pcount = counts;
00042 
00043     switch (num)
00044     {
00045       case 256:
00046       {
00047         *pval = 0;
00048         *pcount = 0;
00049         vmin = 0;
00050         vmax = 255;
00051         break;
00052       }
00053 
00054       case 65536:
00055       {
00056         *pval = 0;
00057         *pcount = 0;
00058         vmin = 0;
00059         vmax = 65535;
00060         break;
00061       }
00062 
00063       default:
00064         break;
00065     }
00066 
00067     for (register int i = 1; i < num; ++i)
00068     {
00069       float  val = *(pval++);
00070       *pval = val + 1;
00071       *(++pcount) = 0;
00072     }
00073   }
00074 }
00075 
00076 void vifa_image_histogram::
00077 fill_histogram(vil_image_view_base_sptr  image,
00078                double                    /* percentage */)
00079 {
00080   // Get the base histogram's array of counts
00081   float*  counts = this->GetCounts();
00082 
00083   switch (form_)
00084   {
00085     case VIL_PIXEL_FORMAT_BYTE:
00086     case VIL_PIXEL_FORMAT_SBYTE:
00087     {
00088       // Cast the abstract image view to a compatible concrete type
00089       vil_image_view<vxl_byte>*  img =
00090                 (vil_image_view<vxl_byte>*)(image.ptr());
00091 
00092       // Are all the pixels in contiguous memory?
00093       if (img->is_contiguous())
00094       {
00095         // Yes - use fast iterator to scan pixels
00096         vxl_byte*    cur_pix = img->begin();
00097         vxl_byte*    last_pix = img->end();
00098         vcl_ptrdiff_t  istep = img->istep();
00099         while (cur_pix != last_pix)
00100         {
00101           counts[*cur_pix]++;
00102           cur_pix += istep;
00103         }
00104       }
00105       else
00106       {
00107         // No - use pixel coordinates
00108         unsigned int  max_j = img->nj();
00109         unsigned int  max_i = img->ni();
00110 
00111         for (unsigned int j = 0; j < max_j; j++)
00112           for (unsigned int i = 0; i < max_i; i++)
00113             counts[(*img)(i, j)]++;
00114       }
00115 
00116       break;
00117     }
00118 
00119     case VIL_PIXEL_FORMAT_UINT_16:
00120     case VIL_PIXEL_FORMAT_INT_16:
00121     {
00122       // Cast the abstract image view to a compatible concrete type
00123       vil_image_view<vxl_int_16>*  img =
00124                 (vil_image_view<vxl_int_16>*)(image.ptr());
00125 
00126       // Are all the pixels in contiguous memory?
00127       if (img->is_contiguous())
00128       {
00129         // Yes - use fast iterator to scan pixels
00130         vxl_int_16*    cur_pix = img->begin();
00131         vxl_int_16*    last_pix = img->end();
00132         vcl_ptrdiff_t  istep = img->istep();
00133         while (cur_pix != last_pix)
00134         {
00135           counts[*cur_pix]++;
00136           cur_pix += istep;
00137         }
00138       }
00139       else
00140       {
00141         // No - use pixel coordinates
00142         unsigned int  max_j = img->nj();
00143         unsigned int  max_i = img->ni();
00144 
00145         for (unsigned int j = 0; j < max_j; j++)
00146           for (unsigned int i = 0; i < max_i; i++)
00147             counts[(*img)(i, j)]++;
00148       }
00149 
00150       break;
00151     }
00152 
00153     default:
00154       break;
00155   }
00156 }