Go to the documentation of this file.00001
00002 #include "vifa_image_histogram.h"
00003
00004
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
00013 form_ = image->pixel_format();
00014 byte_depth_ = vil_pixel_format_sizeof_components(form_) *
00015 vil_pixel_format_num_components(form_);
00016
00017
00018 init();
00019
00020
00021 fill_histogram(image, percentage);
00022 }
00023
00024 void vifa_image_histogram::
00025 init(void)
00026 {
00027 num = 1 << (byte_depth_ * 8);
00028
00029
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 )
00079 {
00080
00081 float* counts = this->GetCounts();
00082
00083 switch (form_)
00084 {
00085 case VIL_PIXEL_FORMAT_BYTE:
00086 case VIL_PIXEL_FORMAT_SBYTE:
00087 {
00088
00089 vil_image_view<vxl_byte>* img =
00090 (vil_image_view<vxl_byte>*)(image.ptr());
00091
00092
00093 if (img->is_contiguous())
00094 {
00095
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
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
00123 vil_image_view<vxl_int_16>* img =
00124 (vil_image_view<vxl_int_16>*)(image.ptr());
00125
00126
00127 if (img->is_contiguous())
00128 {
00129
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
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 }