00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <sstream>
00042
00043 #include <qgarlib/array.H>
00044 #include <qgarlib/ContrastEnhancedImage.H>
00045 #include <qgarlib/GenImage.H>
00046 #include <qgarlib/QgarErrorDomain.H>
00047
00048
00049
00050 namespace qgar
00051 {
00052
00053
00054
00055
00056 ContrastEnhancedImage::ContrastEnhancedImage(GreyLevelImage& anImg,
00057 unsigned int aMaskSize)
00058
00059 throw(QgarErrorDomain)
00060
00061 : GreyLevelImage(anImg)
00062
00063 {
00064 int sqsize = (2 * (int)aMaskSize) + 1;
00065
00066 if ((sqsize > _width) || (sqsize > _height))
00067 {
00068 std::ostringstream os;
00069 os << "Mask ["
00070 << sqsize
00071 << " X "
00072 << sqsize
00073 << "] too large for image ["
00074 << _width
00075 << " X "
00076 << _height
00077 << "]";
00078 throw QgarErrorDomain(__FILE__, __LINE__,
00079 "qgar::ContrastEnhancedImage::ContrastEnhancedImage(qgar::GreyLevelImage&, unsigned int)",
00080 os.str());
00081 }
00082
00083
00084 GreyLevelImage::value_type* ltabmax = new GreyLevelImage::value_type[_width];
00085 GreyLevelImage::value_type* ltabmin = new GreyLevelImage::value_type[_width];
00086
00087
00088 GreyLevelImage::value_type* crow = new GreyLevelImage::value_type[_width];
00089
00090
00091 GreyLevelImage::value_type* orow = new GreyLevelImage::value_type[_width];
00092
00093 GreyLevelImage::value_type* p;
00094 GreyLevelImage::value_type* q;
00095
00096
00097
00098
00099
00100 int i = 0;
00101
00102 for (int l = (int)aMaskSize ; l < _height - (int)aMaskSize ; ++l, ++i)
00103 {
00104
00105 GreyLevelImage::value_type* smin = ltabmin;
00106 GreyLevelImage::value_type* smax = ltabmax;
00107
00108 qgFill(ltabmin, _width, (GreyLevelImage::value_type)255);
00109 qgFill(ltabmax, _width, (GreyLevelImage::value_type) 0);
00110
00111
00112 for (int ii = i ; ii < i + sqsize ; ++ii)
00113 {
00114 anImg.row(ii, crow);
00115 q = crow;
00116 smin = ltabmin;
00117 smax = ltabmax;
00118
00119 for (int j = 0 ; j < _width ; ++j, ++q, ++smin, ++smax)
00120 {
00121 if (*smax < *q)
00122 {
00123 *smax = *q;
00124 }
00125 if (*smin > *q)
00126 {
00127 *smin = *q;
00128 }
00129 }
00130 }
00131
00132
00133
00134
00135 row(l, orow);
00136
00137 p = orow + aMaskSize;
00138 q = crow + aMaskSize;
00139 smin = ltabmin, smax = ltabmax;
00140
00141 for (int j = (int)aMaskSize ;
00142 j < _width - (int)aMaskSize ;
00143 ++j, ++smin, ++smax, ++p, ++q)
00144 {
00145
00146 GreyLevelImage::value_type curmax = 0;
00147 GreyLevelImage::value_type curmin = 255;
00148 GreyLevelImage::value_type* slmax = smax;
00149 GreyLevelImage::value_type* slmin = smin;
00150
00151 for (int k = 0 ; k < sqsize ; ++k, ++slmin, ++slmax)
00152 {
00153 if (curmax < *slmax)
00154 {
00155 curmax = *slmax;
00156 }
00157 if (curmin > *slmin)
00158 {
00159 curmin = *slmin;
00160 }
00161 }
00162
00163
00164 if ((curmax - *q) < (*q - curmin))
00165 {
00166 *p = curmax;
00167 }
00168 else
00169 {
00170 *p = curmin;
00171 }
00172 }
00173
00174
00175 setRow(l, orow);
00176
00177 }
00178
00179
00180 delete [] ltabmin;
00181 delete [] ltabmax;
00182 delete [] crow;
00183 delete [] orow;
00184 }
00185
00186
00187
00188 }