00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006 #include "ByteConvertImage.h"
00007
00008 #include <vnl/vnl_math.h>
00009 #include <vil1/vil1_clamp.h>
00010 #include <vcl_iostream.h>
00011 #include <vxl_config.h>
00012
00013 ByteConvertImage::ByteConvertImage(vil1_memory_image_of<float> const& in, bool ignore_zero):
00014 base(in.width(), in.height())
00015 {
00016 ignore_zero_ = ignore_zero;
00017 min_ = vnl_huge_val(0.0f);
00018 max_ = -min_;
00019 for (int y = 0; y < in.height(); ++y)
00020 for (int x = 0; x < in.width(); ++x) {
00021 float v = in(x,y);
00022 if (ignore_zero && v == 0.0F)
00023 continue;
00024
00025 if (v < min_) min_ = v;
00026 if (v > max_) max_ = v;
00027 }
00028
00029 filter(in);
00030 }
00031
00032 ByteConvertImage::ByteConvertImage(vil1_memory_image_of<float> const& in, float min, float max):
00033 base(in.width(), in.height())
00034 {
00035 ignore_zero_ = false;
00036 min_ = min;
00037 max_ = max;
00038
00039 filter(in);
00040 }
00041
00042 ByteConvertImage::ByteConvertImage(vil1_memory_image_of<double> const& in, bool ignore_zero):
00043 base(in.width(), in.height())
00044 {
00045 ignore_zero_ = ignore_zero;
00046 min_ = (float)HUGE_VAL;
00047 max_ = -min_;
00048 for (int y = 0; y < in.height(); ++y)
00049 for (int x = 0; x < in.width(); ++x) {
00050 double v = in(x,y);
00051 if (ignore_zero && v == 0.0F)
00052 continue;
00053
00054 if ((float)v < min_) min_ = (float)v;
00055 if ((float)v > max_) max_ = (float)v;
00056 }
00057
00058 filter(in);
00059 }
00060
00061 ByteConvertImage::ByteConvertImage(vil1_memory_image_of<double> const& in, float min, float max):
00062 base(in.width(), in.height())
00063 {
00064 ignore_zero_ = false;
00065 min_ = min;
00066 max_ = max;
00067
00068 filter(in);
00069 }
00070
00071 void ByteConvertImage::filter(vil1_memory_image_of<float> const& in)
00072 {
00073 float scale = 255 / (max_ - min_);
00074 for (int y = 0; y < in.height(); ++y)
00075 for (int x = 0; x < in.width(); ++x) {
00076 float v = in(x,y);
00077 if (ignore_zero_ && v == 0.0F)
00078 (*this)(x,y) = 0;
00079 else
00080 (*this)(x,y) = (vxl_byte)vil1_clamp_pixel((v - min_) * scale, 0, 255);
00081 }
00082 }
00083
00084 void ByteConvertImage::filter(vil1_memory_image_of<double> const& in)
00085 {
00086 double dmin = (double)min_;
00087 double scale = 255 / (max_ - min_);
00088 for (int y = 0; y < in.height(); ++y)
00089 for (int x = 0; x < in.width(); ++x) {
00090 double v = in(x,y);
00091 if (ignore_zero_ && v == 0.0F)
00092 (*this)(x,y) = 0;
00093 else
00094 (*this)(x,y) = (vxl_byte)vil1_clamp_pixel((v - dmin) * scale, 0, 255);
00095 }
00096 }
00097
00098 void ByteConvertImage::print(vcl_ostream& s) const
00099 {
00100 s << "ByteConvertImage: range " << min_ << ".." << max_ << '\n';
00101 }