Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vifa_image_histogram.cxx

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

Generated on Thu Jan 10 14:47:30 2008 for contrib/gel/vifa by  doxygen 1.4.4