Commit c2ed90ec authored by Davis King's avatar Davis King

Changed the pixel_traits so that signed grayscale pixel types are allowed. This

involved adding a few new fields to pixel_traits.  I also changed the get_pixel_intensity()
function so that its return value is of the same type as the basic pixel type rather
than always being unsigned long.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403967
parent b7c2625c
...@@ -129,7 +129,10 @@ namespace dlib ...@@ -129,7 +129,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = false; const static bool hsi = false;
const static long num = 3; const static long num = 3;
static unsigned long max() { return 255;} typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = false; const static bool has_alpha = false;
}; };
......
...@@ -145,7 +145,10 @@ namespace dlib ...@@ -145,7 +145,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = false; const static bool hsi = false;
const static long num = 3; const static long num = 3;
static unsigned long max() { return 255;} typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = false; const static bool has_alpha = false;
}; };
......
...@@ -28,7 +28,7 @@ namespace dlib ...@@ -28,7 +28,7 @@ namespace dlib
/*! /*!
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
As the name implies, this is a traits class for pixel types. As the name implies, this is a traits class for pixel types.
It defines the properties of a pixel (duah). It defines the properties of a pixel.
This traits class will define the following public static members: This traits class will define the following public static members:
- bool grayscale - bool grayscale
...@@ -39,7 +39,11 @@ namespace dlib ...@@ -39,7 +39,11 @@ namespace dlib
- bool has_alpha - bool has_alpha
- long num - long num
- unsigned long max()
- basic_pixel_type
- basic_pixel_type min()
- basic_pixel_type max()
- is_unsigned
The above public constants are subject to the following constraints: The above public constants are subject to the following constraints:
- only one of grayscale, rgb, rgb_alpha, or hsi is true - only one of grayscale, rgb, rgb_alpha, or hsi is true
...@@ -49,7 +53,10 @@ namespace dlib ...@@ -49,7 +53,10 @@ namespace dlib
- This type of pixel represents the RGB color space. - This type of pixel represents the RGB color space.
- num == 3 - num == 3
- has_alpha == false - has_alpha == false
- basic_pixel_type == unsigned char
- min() == 0
- max() == 255 - max() == 255
- is_unsigned == true
- if (rgb_alpha == true) then - if (rgb_alpha == true) then
- The type T will be a struct with 4 public members of type - The type T will be a struct with 4 public members of type
unsigned char named "red" "green" "blue" and "alpha". unsigned char named "red" "green" "blue" and "alpha".
...@@ -59,21 +66,30 @@ namespace dlib ...@@ -59,21 +66,30 @@ namespace dlib
with maximum opacity. with maximum opacity.
- num == 4 - num == 4
- has_alpha == true - has_alpha == true
- basic_pixel_type == unsigned char
- min() == 0
- max() == 255 - max() == 255
- is_unsigned == true
- else if (hsi == true) then - else if (hsi == true) then
- The type T will be a struct with 3 public members of type - The type T will be a struct with 3 public members of type
unsigned char named "h" "s" and "i". unsigned char named "h" "s" and "i".
- This type of pixel represents the HSI color space. - This type of pixel represents the HSI color space.
- num == 3 - num == 3
- has_alpha == false - has_alpha == false
- basic_pixel_type == unsigned char
- min() == 0
- max() == 255 - max() == 255
- is_unsigned == true
- else - else
- grayscale == true - grayscale == true
- The type T will be an unsigned integral type. - This type of pixel represents a grayscale color space. T
- This type of pixel represents a grayscale color space will be some kind of basic scalar type such as unsigned int.
- num == 1 - num == 1
- has_alpha == false - has_alpha == false
- max() == std::numeric_limits<T>::max() - basic_pixel_type == T
- min() == the minimum obtainable value of objects of type T
- max() == the maximum obtainable value of objects of type T
- is_unsigned is true if min() == 0 and false otherwise
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -190,13 +206,14 @@ namespace dlib ...@@ -190,13 +206,14 @@ namespace dlib
- pixel_traits<P2> must be defined - pixel_traits<P2> must be defined
ensures ensures
- if (P1 and P2 are the same type of pixel) then - if (P1 and P2 are the same type of pixel) then
- simply coppies the value of src into dest. In other words, - simply copies the value of src into dest. In other words,
dest will be identical to src after this function returns. dest will be identical to src after this function returns.
- else if (P1 and P2 are not the same type of pixel) then - else if (P1 and P2 are not the same type of pixel) then
- assigns pixel src to pixel dest and does any necessary color space - assigns pixel src to pixel dest and does any necessary color space
conversions. conversions.
- When converting from a grayscale color space with more than 255 values the - When converting from a grayscale color space with more than 255 values the
pixel intensity is saturated at pixel_traits<P1>::max(). pixel intensity is saturated at pixel_traits<P1>::max() or pixel_traits<P1>::min()
as appropriate.
- if (the dest pixel has an alpha channel and the src pixel doesn't) then - if (the dest pixel has an alpha channel and the src pixel doesn't) then
- #dest.alpha == 255 - #dest.alpha == 255
- else if (the src pixel has an alpha channel but the dest pixel doesn't) then - else if (the src pixel has an alpha channel but the dest pixel doesn't) then
...@@ -205,40 +222,12 @@ namespace dlib ...@@ -205,40 +222,12 @@ namespace dlib
(i.e. #dest == src*(alpha/255) + dest*(1-alpha/255)) (i.e. #dest == src*(alpha/255) + dest*(1-alpha/255))
!*/ !*/
template <
typename P
>
inline void assign_pixel (
P& dest,
const int src
);
/*!
requires
- pixel_traits<P> must be defined
ensures
- performs assign_pixel(dest, static_cast<unsigned long>(max(0,src)))
!*/
template <
typename P
>
inline void assign_pixel (
P& dest,
const long src
);
/*!
requires
- pixel_traits<P> must be defined
ensures
- performs assign_pixel(dest, static_cast<unsigned long>(max(0,src)))
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
typename P typename P
> >
inline unsigned long get_pixel_intensity ( inline typename pixel_traits<P>::basic_pixel_type get_pixel_intensity (
const P& src const P& src
); );
/*! /*!
...@@ -254,18 +243,21 @@ namespace dlib ...@@ -254,18 +243,21 @@ namespace dlib
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
typename P typename P,
typename T
> >
inline void assign_pixel_intensity ( inline void assign_pixel_intensity (
P& dest, P& dest,
const unsigned long new_intensity const T& new_intensity
); );
/*! /*!
requires requires
- pixel_traits<P> must be defined - pixel_traits<P> must be defined
- pixel_traits<T> must be defined
ensures ensures
- let val == min(new_intensity, pixel_traits<P>::max()) - #get_pixel_intensity(dest) == get_pixel_intensity(new_intensity)
- #get_pixel_intensity(dest) == val (or if get_pixel_intensity(new_intensity) can't be represented by
dest then the closest value representable by dest is used)
- if (the dest pixel has an alpha channel) then - if (the dest pixel has an alpha channel) then
- #dest.alpha == dest.alpha - #dest.alpha == dest.alpha
!*/ !*/
...@@ -360,7 +352,10 @@ namespace dlib ...@@ -360,7 +352,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = false; const static bool hsi = false;
const static long num = 3; const static long num = 3;
static unsigned long max() { return 255;} typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = false; const static bool has_alpha = false;
}; };
...@@ -374,7 +369,10 @@ namespace dlib ...@@ -374,7 +369,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = false; const static bool hsi = false;
const static long num = 3; const static long num = 3;
static unsigned long max() { return 255;} typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = false; const static bool has_alpha = false;
}; };
...@@ -388,7 +386,10 @@ namespace dlib ...@@ -388,7 +386,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = false; const static bool hsi = false;
const static long num = 4; const static long num = 4;
static unsigned long max() { return 255; } typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = true; const static bool has_alpha = true;
}; };
...@@ -403,7 +404,10 @@ namespace dlib ...@@ -403,7 +404,10 @@ namespace dlib
const static bool grayscale = false; const static bool grayscale = false;
const static bool hsi = true; const static bool hsi = true;
const static long num = 3; const static long num = 3;
static unsigned long max() { return 255;} typedef unsigned char basic_pixel_type;
static basic_pixel_type min() { return 0;}
static basic_pixel_type max() { return 255;}
const static bool is_unsigned = true;
const static bool has_alpha = false; const static bool has_alpha = false;
}; };
...@@ -418,7 +422,10 @@ namespace dlib ...@@ -418,7 +422,10 @@ namespace dlib
const static bool hsi = false; const static bool hsi = false;
const static long num = 1; const static long num = 1;
const static bool has_alpha = false; const static bool has_alpha = false;
static unsigned long max() { return std::numeric_limits<T>::max();} typedef T basic_pixel_type;
static basic_pixel_type min() { return std::numeric_limits<T>::min();}
static basic_pixel_type max() { return std::numeric_limits<T>::max();}
const static bool is_unsigned = is_unsigned_type<T>::value;
}; };
template <> struct pixel_traits<unsigned char> : public grayscale_pixel_traits<unsigned char> {}; template <> struct pixel_traits<unsigned char> : public grayscale_pixel_traits<unsigned char> {};
...@@ -426,6 +433,36 @@ namespace dlib ...@@ -426,6 +433,36 @@ namespace dlib
template <> struct pixel_traits<unsigned int> : public grayscale_pixel_traits<unsigned int> {}; template <> struct pixel_traits<unsigned int> : public grayscale_pixel_traits<unsigned int> {};
template <> struct pixel_traits<unsigned long> : public grayscale_pixel_traits<unsigned long> {}; template <> struct pixel_traits<unsigned long> : public grayscale_pixel_traits<unsigned long> {};
template <> struct pixel_traits<char> : public grayscale_pixel_traits<char> {};
template <> struct pixel_traits<signed char> : public grayscale_pixel_traits<signed char> {};
template <> struct pixel_traits<short> : public grayscale_pixel_traits<short> {};
template <> struct pixel_traits<int> : public grayscale_pixel_traits<int> {};
template <> struct pixel_traits<long> : public grayscale_pixel_traits<long> {};
template <> struct pixel_traits<int64> : public grayscale_pixel_traits<int64> {};
template <> struct pixel_traits<uint64> : public grayscale_pixel_traits<uint64> {};
// ----------------------------------------------------------------------------------------
template <typename T>
struct float_grayscale_pixel_traits
{
const static bool rgb = false;
const static bool rgb_alpha = false;
const static bool grayscale = true;
const static bool hsi = false;
const static long num = 1;
const static bool has_alpha = false;
typedef T basic_pixel_type;
static basic_pixel_type min() { return -std::numeric_limits<T>::max();}
static basic_pixel_type max() { return std::numeric_limits<T>::max();}
const static bool is_unsigned = false;
};
template <> struct pixel_traits<float> : public float_grayscale_pixel_traits<float> {};
template <> struct pixel_traits<double> : public float_grayscale_pixel_traits<double> {};
template <> struct pixel_traits<long double> : public float_grayscale_pixel_traits<long double> {};
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// The following is a bunch of conversion stuff for the assign_pixel function. // The following is a bunch of conversion stuff for the assign_pixel function.
...@@ -443,14 +480,66 @@ namespace dlib ...@@ -443,14 +480,66 @@ namespace dlib
dest = src; dest = src;
} }
// -----------------------------
template <typename T, typename P>
typename enable_if_c<(sizeof(P) < sizeof(T)), bool>::type less_or_equal_to_max (
const P&
) { return true; }
/*!
ensures
- returns true if p is <= max value of T
!*/
template <typename T, typename P>
typename enable_if_c<sizeof(P) == sizeof(T), bool>::type less_or_equal_to_max (
const P& p
)
{
return p <= pixel_traits<T>::max();
}
template <typename T, typename P>
typename enable_if_c<(sizeof(P) > sizeof(T)), bool>::type less_or_equal_to_max (
const P& p
)
{
return p <= static_cast<P>(pixel_traits<T>::max());
}
template <typename T, typename P>
typename enable_if<is_unsigned_type<P>, bool >::type greater_or_equal_to_min (
const P&
) { return true; }
/*!
ensures
- returns true if p is >= min value of T
!*/
template <typename T, typename P>
typename disable_if<is_unsigned_type<P>, bool >::type greater_or_equal_to_min (
const P& p
)
{
return p >= pixel_traits<T>::min();
}
template < typename P1, typename P2 > template < typename P1, typename P2 >
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::grayscale>::type typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::grayscale>::type
assign(P1& dest, const P2& src) assign(P1& dest, const P2& src)
{ {
if (src <= pixel_traits<P1>::max()) /*
The reason for these weird comparison functions is to avoid getting compiler
warnings about comparing signed types to unsigned and stuff like that.
*/
if (less_or_equal_to_max<P1>(src))
if (greater_or_equal_to_min<P1>(src))
dest = static_cast<P1>(src); dest = static_cast<P1>(src);
else else
dest = static_cast<P1>(pixel_traits<P1>::max()); dest = pixel_traits<P1>::min();
else
dest = pixel_traits<P1>::max();
} }
template < typename P1, typename P2 > template < typename P1, typename P2 >
...@@ -488,9 +577,10 @@ namespace dlib ...@@ -488,9 +577,10 @@ namespace dlib
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb>::type typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb>::type
assign(P1& dest, const P2& src) assign(P1& dest, const P2& src)
{ {
dest = static_cast<P1>((static_cast<unsigned int>(src.red) + const unsigned int temp = ((static_cast<unsigned int>(src.red) +
static_cast<unsigned int>(src.green) + static_cast<unsigned int>(src.green) +
static_cast<unsigned int>(src.blue))/3); static_cast<unsigned int>(src.blue))/3);
assign_pixel(dest, temp);
} }
template < typename P1, typename P2 > template < typename P1, typename P2 >
...@@ -504,26 +594,26 @@ namespace dlib ...@@ -504,26 +594,26 @@ namespace dlib
if (src.alpha == 255) if (src.alpha == 255)
{ {
dest = avg; assign_pixel(dest, avg);
} }
else else
{ {
// perform this assignment using fixed point arithmetic: // perform this assignment using fixed point arithmetic:
// dest = src*(alpha/255) + src*(1 - alpha/255); // dest = src*(alpha/255) + dest*(1 - alpha/255);
// dest = src*(alpha/255) + dest*1 - dest*(alpha/255); // dest = src*(alpha/255) + dest*1 - dest*(alpha/255);
// dest = dest*1 + src*(alpha/255) - dest*(alpha/255); // dest = dest*1 + src*(alpha/255) - dest*(alpha/255);
// dest = dest*1 + (src - dest)*(alpha/255); // dest = dest*1 + (src - dest)*(alpha/255);
// dest += (src - dest)*(alpha/255); // dest += (src - dest)*(alpha/255);
unsigned int temp = avg; int temp = avg;
temp -= dest; temp -= dest;
temp *= src.alpha; temp *= src.alpha;
temp >>= 8; temp /= 255;
dest += static_cast<unsigned char>(temp&0xFF); assign_pixel(dest, temp+dest);
} }
} }
...@@ -531,7 +621,7 @@ namespace dlib ...@@ -531,7 +621,7 @@ namespace dlib
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::hsi>::type typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::hsi>::type
assign(P1& dest, const P2& src) assign(P1& dest, const P2& src)
{ {
dest = static_cast<P1>(src.i); assign_pixel(dest, src.i);
} }
...@@ -650,11 +740,7 @@ namespace dlib ...@@ -650,11 +740,7 @@ namespace dlib
assign(P1& dest, const P2& src) assign(P1& dest, const P2& src)
{ {
unsigned char p; unsigned char p;
if (src <= 255) assign_pixel(p, src);
p = static_cast<unsigned char>(src);
else
p = 255;
dest.red = p; dest.red = p;
dest.green = p; dest.green = p;
dest.blue = p; dest.blue = p;
...@@ -737,10 +823,7 @@ namespace dlib ...@@ -737,10 +823,7 @@ namespace dlib
assign(P1& dest, const P2& src) assign(P1& dest, const P2& src)
{ {
unsigned char p; unsigned char p;
if (src <= 255) assign_pixel(p, src);
p = static_cast<unsigned char>(src);
else
p = 255;
dest.red = p; dest.red = p;
dest.green = p; dest.green = p;
...@@ -795,10 +878,7 @@ namespace dlib ...@@ -795,10 +878,7 @@ namespace dlib
{ {
dest.h = 0; dest.h = 0;
dest.s = 0; dest.s = 0;
if (src <= 255) assign_pixel(dest.i, src);
dest.i = static_cast<unsigned char>(src);
else
dest.i = 255;
} }
template < typename P1, typename P2 > template < typename P1, typename P2 >
...@@ -843,56 +923,28 @@ namespace dlib ...@@ -843,56 +923,28 @@ namespace dlib
const P2& src const P2& src
) { assign_pixel_helpers::assign(dest,src); } ) { assign_pixel_helpers::assign(dest,src); }
template < typename P1>
inline void assign_pixel (
P1& dest,
const int src
)
{
unsigned long p;
if (src >= 0)
p = static_cast<unsigned long>(src);
else
p = 0;
assign_pixel(dest, p);
}
template < typename P1>
inline void assign_pixel (
P1& dest,
const long src
)
{
unsigned long p;
if (src >= 0)
p = static_cast<unsigned long>(src);
else
p = 0;
assign_pixel(dest, p);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <
typename P typename P,
typename T
> >
inline typename enable_if_c<pixel_traits<P>::grayscale>::type assign_pixel_intensity_helper ( inline typename enable_if_c<pixel_traits<P>::grayscale>::type assign_pixel_intensity_helper (
P& dest, P& dest,
const unsigned long& new_intensity const T& new_intensity
) )
{ {
assign_pixel(dest, new_intensity); assign_pixel(dest, new_intensity);
} }
template < template <
typename P typename P,
typename T
> >
inline typename enable_if_c<pixel_traits<P>::grayscale == false&& inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
pixel_traits<P>::has_alpha>::type assign_pixel_intensity_helper ( pixel_traits<P>::has_alpha>::type assign_pixel_intensity_helper (
P& dest, P& dest,
const unsigned long& new_intensity const T& new_intensity
) )
{ {
unsigned long alpha = dest.alpha; unsigned long alpha = dest.alpha;
...@@ -904,12 +956,13 @@ namespace dlib ...@@ -904,12 +956,13 @@ namespace dlib
} }
template < template <
typename P typename P,
typename T
> >
inline typename enable_if_c<pixel_traits<P>::grayscale == false&& inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
pixel_traits<P>::has_alpha == false>::type assign_pixel_intensity_helper ( pixel_traits<P>::has_alpha == false>::type assign_pixel_intensity_helper (
P& dest, P& dest,
const unsigned long& new_intensity const T& new_intensity
) )
{ {
hsi_pixel p; hsi_pixel p;
...@@ -919,11 +972,12 @@ namespace dlib ...@@ -919,11 +972,12 @@ namespace dlib
} }
template < template <
typename P typename P,
typename T
> >
inline void assign_pixel_intensity ( inline void assign_pixel_intensity (
P& dest, P& dest,
const unsigned long new_intensity const T& new_intensity
) )
{ {
assign_pixel_intensity_helper(dest, new_intensity); assign_pixel_intensity_helper(dest, new_intensity);
...@@ -934,18 +988,19 @@ namespace dlib ...@@ -934,18 +988,19 @@ namespace dlib
template < template <
typename P typename P
> >
inline typename enable_if_c<pixel_traits<P>::grayscale,unsigned long>::type get_pixel_intensity_helper ( inline typename enable_if_c<pixel_traits<P>::grayscale, P>::type get_pixel_intensity_helper (
const P& src const P& src
) )
{ {
return static_cast<unsigned long>(src); return src;
} }
template < template <
typename P typename P
> >
inline typename enable_if_c<pixel_traits<P>::grayscale == false&& inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
pixel_traits<P>::has_alpha, unsigned long>::type get_pixel_intensity_helper ( pixel_traits<P>::has_alpha,
typename pixel_traits<P>::basic_pixel_type>::type get_pixel_intensity_helper (
const P& src const P& src
) )
{ {
...@@ -953,26 +1008,27 @@ namespace dlib ...@@ -953,26 +1008,27 @@ namespace dlib
temp.alpha = 255; temp.alpha = 255;
hsi_pixel p; hsi_pixel p;
assign_pixel(p,temp); assign_pixel(p,temp);
return static_cast<unsigned long>(p.i); return static_cast<typename pixel_traits<P>::basic_pixel_type>(p.i);
} }
template < template <
typename P typename P
> >
inline typename enable_if_c<pixel_traits<P>::grayscale == false&& inline typename enable_if_c<pixel_traits<P>::grayscale == false&&
pixel_traits<P>::has_alpha == false, unsigned long>::type get_pixel_intensity_helper ( pixel_traits<P>::has_alpha == false,
typename pixel_traits<P>::basic_pixel_type>::type get_pixel_intensity_helper (
const P& src const P& src
) )
{ {
hsi_pixel p; hsi_pixel p;
assign_pixel(p,src); assign_pixel(p,src);
return static_cast<unsigned long>(p.i); return static_cast<typename pixel_traits<P>::basic_pixel_type>(p.i);
} }
template < template <
typename P typename P
> >
inline unsigned long get_pixel_intensity ( inline typename pixel_traits<P>::basic_pixel_type get_pixel_intensity (
const P& src const P& src
) )
{ {
......
...@@ -32,17 +32,27 @@ namespace ...@@ -32,17 +32,27 @@ namespace
unsigned char p_gray; unsigned char p_gray;
unsigned short p_gray16; unsigned short p_gray16;
long p_int;
float p_float;
signed char p_schar;
rgb_pixel p_rgb; rgb_pixel p_rgb;
hsi_pixel p_hsi, p_hsi2; hsi_pixel p_hsi, p_hsi2;
rgb_alpha_pixel p_rgba; rgb_alpha_pixel p_rgba;
assign_pixel(p_int, 0.0f);
assign_pixel(p_float, 0.0f);
assign_pixel(p_schar, 0);
assign_pixel(p_gray,0); assign_pixel(p_gray, -2);
assign_pixel(p_rgb,0); assign_pixel(p_rgb,0);
assign_pixel(p_hsi,0); assign_pixel(p_hsi, -4);
assign_pixel(p_rgba,0); assign_pixel(p_rgba, p_int);
assign_pixel(p_gray16,0); assign_pixel(p_gray16,0);
DLIB_TEST(p_int == 0);
DLIB_TEST(p_float == 0);
DLIB_TEST(p_schar == 0);
DLIB_TEST(p_gray == 0); DLIB_TEST(p_gray == 0);
DLIB_TEST(p_gray16 == 0); DLIB_TEST(p_gray16 == 0);
...@@ -65,6 +75,14 @@ namespace ...@@ -65,6 +75,14 @@ namespace
assign_pixel(p_hsi,10); assign_pixel(p_hsi,10);
assign_pixel(p_rgba,10); assign_pixel(p_rgba,10);
assign_pixel(p_int, -10);
assign_pixel(p_float, -10);
assign_pixel(p_schar, -10);
DLIB_TEST(p_int == -10);
DLIB_TEST(p_float == -10);
DLIB_TEST(p_schar == -10);
DLIB_TEST(p_gray == 10); DLIB_TEST(p_gray == 10);
DLIB_TEST(p_gray16 == 10); DLIB_TEST(p_gray16 == 10);
...@@ -84,6 +102,9 @@ namespace ...@@ -84,6 +102,9 @@ namespace
assign_pixel(p_gray16,12345); assign_pixel(p_gray16,12345);
DLIB_TEST(p_gray16 == 12345); DLIB_TEST(p_gray16 == 12345);
assign_pixel(p_float,3.141);
DLIB_TEST(p_float == 3.141f);
p_rgb.red = 255; p_rgb.red = 255;
p_rgb.green = 100; p_rgb.green = 100;
p_rgb.blue = 50; p_rgb.blue = 50;
...@@ -94,6 +115,14 @@ namespace ...@@ -94,6 +115,14 @@ namespace
assign_pixel(p_rgba,p_rgb); assign_pixel(p_rgba,p_rgb);
assign_pixel(p_hsi,p_rgb); assign_pixel(p_hsi,p_rgb);
assign_pixel(p_float,p_rgb);
assign_pixel(p_int,p_rgb);
assign_pixel(p_schar,p_rgb);
DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
DLIB_TEST(p_int == (255+100+50)/3);
DLIB_TEST(p_float == (255+100+50)/3);
DLIB_TEST(p_gray == (255+100+50)/3); DLIB_TEST(p_gray == (255+100+50)/3);
DLIB_TEST(p_rgb.red == 255); DLIB_TEST(p_rgb.red == 255);
...@@ -138,9 +167,19 @@ namespace ...@@ -138,9 +167,19 @@ namespace
assign_pixel(p_rgb, 10); assign_pixel(p_rgb, 10);
assign_pixel(p_hsi, 10); assign_pixel(p_hsi, 10);
assign_pixel(p_schar, 10);
assign_pixel(p_float, 10);
assign_pixel(p_int, 10);
p_rgba.alpha = 0; p_rgba.alpha = 0;
assign_pixel(p_gray, p_rgba); assign_pixel(p_gray, p_rgba);
DLIB_TEST(p_gray == 10); DLIB_TEST(p_gray == 10);
assign_pixel(p_schar, p_rgba);
DLIB_TEST(p_schar == 10);
assign_pixel(p_int, p_rgba);
DLIB_TEST(p_int == 10);
assign_pixel(p_float, p_rgba);
DLIB_TEST(p_float == 10);
assign_pixel(p_rgb, p_rgba); assign_pixel(p_rgb, p_rgba);
DLIB_TEST(p_rgb.red == 10); DLIB_TEST(p_rgb.red == 10);
DLIB_TEST(p_rgb.green == 10); DLIB_TEST(p_rgb.green == 10);
...@@ -154,22 +193,41 @@ namespace ...@@ -154,22 +193,41 @@ namespace
assign_pixel(p_rgba, 100); assign_pixel(p_rgba, 100);
assign_pixel(p_gray, 10); assign_pixel(p_gray, 10);
assign_pixel(p_schar, 10);
assign_pixel(p_float, 10);
assign_pixel(p_int, 10);
assign_pixel(p_rgb, 10); assign_pixel(p_rgb, 10);
p_rgba.alpha = 128; p_rgba.alpha = 128;
assign_pixel(p_gray, p_rgba); assign_pixel(p_gray, p_rgba);
assign_pixel(p_schar, p_rgba);
assign_pixel(p_float, p_rgba);
assign_pixel(p_int, p_rgba);
assign_pixel(p_rgb, p_rgba); assign_pixel(p_rgb, p_rgba);
DLIB_TEST(p_gray == (100 + 10)/2); DLIB_TEST(p_gray == (100 + 10)/2);
DLIB_TEST(p_schar == (100 + 10)/2);
DLIB_TEST(p_int == (100 + 10)/2);
DLIB_TEST(p_float == (100 + 10)/2);
DLIB_TEST(p_rgb.red == (100 + 10)/2); DLIB_TEST(p_rgb.red == (100 + 10)/2);
DLIB_TEST(p_rgb.green == (100 + 10)/2); DLIB_TEST(p_rgb.green == (100 + 10)/2);
DLIB_TEST(p_rgb.blue == (100 + 10)/2); DLIB_TEST(p_rgb.blue == (100 + 10)/2);
assign_pixel(p_rgba, 100); assign_pixel(p_rgba, 100);
assign_pixel(p_gray, 10); assign_pixel(p_gray, 10);
assign_pixel(p_schar, 10);
assign_pixel(p_int, 10);
assign_pixel(p_float, 10);
assign_pixel(p_rgb, 10); assign_pixel(p_rgb, 10);
DLIB_TEST(p_rgba.alpha == 255); DLIB_TEST(p_rgba.alpha == 255);
assign_pixel(p_gray, p_rgba); assign_pixel(p_gray, p_rgba);
assign_pixel(p_schar, p_rgba);
assign_pixel(p_int, p_rgba);
assign_pixel(p_float, p_rgba);
assign_pixel(p_rgb, p_rgba); assign_pixel(p_rgb, p_rgba);
DLIB_TEST(p_gray == 100); DLIB_TEST(p_gray == 100);
DLIB_TEST(p_schar == 100);
DLIB_TEST(p_int == 100);
DLIB_TEST(p_float == 100);
DLIB_TEST(p_rgb.red == 100); DLIB_TEST(p_rgb.red == 100);
DLIB_TEST(p_rgb.green == 100); DLIB_TEST(p_rgb.green == 100);
DLIB_TEST(p_rgb.blue == 100); DLIB_TEST(p_rgb.blue == 100);
...@@ -185,6 +243,9 @@ namespace ...@@ -185,6 +243,9 @@ namespace
p_rgba.alpha = 7; p_rgba.alpha = 7;
p_gray = 8; p_gray = 8;
p_schar = 8;
p_int = 8;
p_float = 8;
p_hsi.h = 9; p_hsi.h = 9;
p_hsi.s = 10; p_hsi.s = 10;
...@@ -194,11 +255,17 @@ namespace ...@@ -194,11 +255,17 @@ namespace
serialize(p_rgb,sout); serialize(p_rgb,sout);
serialize(p_rgba,sout); serialize(p_rgba,sout);
serialize(p_gray,sout); serialize(p_gray,sout);
serialize(p_schar,sout);
serialize(p_int,sout);
serialize(p_float,sout);
serialize(p_hsi,sout); serialize(p_hsi,sout);
assign_pixel(p_rgb,0); assign_pixel(p_rgb,0);
assign_pixel(p_rgba,0); assign_pixel(p_rgba,0);
assign_pixel(p_gray,0); assign_pixel(p_gray,0);
assign_pixel(p_schar,0);
assign_pixel(p_int,0);
assign_pixel(p_float,0);
assign_pixel(p_hsi,0); assign_pixel(p_hsi,0);
istringstream sin(sout.str()); istringstream sin(sout.str());
...@@ -206,6 +273,9 @@ namespace ...@@ -206,6 +273,9 @@ namespace
deserialize(p_rgb,sin); deserialize(p_rgb,sin);
deserialize(p_rgba,sin); deserialize(p_rgba,sin);
deserialize(p_gray,sin); deserialize(p_gray,sin);
deserialize(p_schar,sin);
deserialize(p_int,sin);
deserialize(p_float,sin);
deserialize(p_hsi,sin); deserialize(p_hsi,sin);
DLIB_TEST(p_rgb.red == 1); DLIB_TEST(p_rgb.red == 1);
...@@ -218,20 +288,31 @@ namespace ...@@ -218,20 +288,31 @@ namespace
DLIB_TEST(p_rgba.alpha == 7); DLIB_TEST(p_rgba.alpha == 7);
DLIB_TEST(p_gray == 8); DLIB_TEST(p_gray == 8);
DLIB_TEST(p_schar == 8);
DLIB_TEST(p_int == 8);
DLIB_TEST(p_float == 8);
DLIB_TEST(p_hsi.h == 9); DLIB_TEST(p_hsi.h == 9);
DLIB_TEST(p_hsi.s == 10); DLIB_TEST(p_hsi.s == 10);
DLIB_TEST(p_hsi.i == 11); DLIB_TEST(p_hsi.i == 11);
{ {
matrix<double,1,1> m_gray; matrix<double,1,1> m_gray, m_schar, m_int, m_float;
matrix<double,3,1> m_rgb, m_hsi; matrix<double,3,1> m_rgb, m_hsi;
m_gray = pixel_to_vector<double>(p_gray); m_gray = pixel_to_vector<double>(p_gray);
m_schar = pixel_to_vector<double>(p_schar);
m_int = pixel_to_vector<double>(p_int);
m_float = pixel_to_vector<double>(p_float);
m_hsi = pixel_to_vector<double>(p_hsi); m_hsi = pixel_to_vector<double>(p_hsi);
m_rgb = pixel_to_vector<double>(p_rgb); m_rgb = pixel_to_vector<double>(p_rgb);
DLIB_TEST(m_gray(0) == p_gray); DLIB_TEST(m_gray(0) == p_gray);
DLIB_TEST(m_float(0) == p_float);
DLIB_TEST(m_int(0) == p_int);
DLIB_TEST(m_schar(0) == p_schar);
DLIB_TEST(m_rgb(0) == p_rgb.red); DLIB_TEST(m_rgb(0) == p_rgb.red);
DLIB_TEST(m_rgb(1) == p_rgb.green); DLIB_TEST(m_rgb(1) == p_rgb.green);
DLIB_TEST(m_rgb(2) == p_rgb.blue); DLIB_TEST(m_rgb(2) == p_rgb.blue);
...@@ -249,6 +330,9 @@ namespace ...@@ -249,6 +330,9 @@ namespace
DLIB_TEST(p_rgba.alpha == 7); DLIB_TEST(p_rgba.alpha == 7);
DLIB_TEST(p_gray == 8); DLIB_TEST(p_gray == 8);
DLIB_TEST(p_int == 8);
DLIB_TEST(p_float == 8);
DLIB_TEST(p_schar == 8);
DLIB_TEST(p_hsi.h == 9); DLIB_TEST(p_hsi.h == 9);
DLIB_TEST(p_hsi.s == 10); DLIB_TEST(p_hsi.s == 10);
...@@ -278,6 +362,126 @@ namespace ...@@ -278,6 +362,126 @@ namespace
DLIB_TEST(p_hsi.i == 11); DLIB_TEST(p_hsi.i == 11);
} }
{
unsigned char p_gray;
unsigned short p_gray16;
long p_int;
float p_float;
signed char p_schar;
rgb_pixel p_rgb;
hsi_pixel p_hsi, p_hsi2;
rgb_alpha_pixel p_rgba;
assign_pixel(p_gray, 0);
assign_pixel(p_gray16, 0);
assign_pixel(p_int, 0);
assign_pixel(p_float, 0);
assign_pixel(p_schar, 0);
assign_pixel(p_rgb, 0);
assign_pixel(p_hsi, 0);
assign_pixel(p_gray, 100);
assign_pixel(p_schar, p_gray);
DLIB_TEST(p_schar == 100);
assign_pixel(p_gray, 200);
assign_pixel(p_schar, p_gray);
DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
assign_pixel(p_int, p_gray);
DLIB_TEST(p_int == 200);
assign_pixel(p_float, p_gray);
DLIB_TEST(p_float == 200);
assign_pixel(p_rgb, p_float);
DLIB_TEST(p_rgb.red == 200);
DLIB_TEST(p_rgb.green == 200);
DLIB_TEST(p_rgb.blue == 200);
p_schar = 0;
assign_pixel(p_schar, p_rgb);
DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
p_schar = -10;
assign_pixel(p_float, p_schar);
DLIB_TEST(p_float == -10);
assign_pixel(p_int, p_schar);
DLIB_TEST(p_int == -10);
assign_pixel(p_schar, p_schar);
DLIB_TEST(p_schar == -10);
assign_pixel(p_gray, p_schar);
DLIB_TEST(p_gray == 0);
assign_pixel(p_rgb, p_schar);
DLIB_TEST(p_rgb.red == 0);
DLIB_TEST(p_rgb.green == 0);
DLIB_TEST(p_rgb.blue == 0);
assign_pixel(p_gray16, p_schar);
DLIB_TEST(p_gray16 == 0);
DLIB_TEST(get_pixel_intensity(p_float) == -10);
DLIB_TEST(get_pixel_intensity(p_int) == -10);
DLIB_TEST(get_pixel_intensity(p_schar) == -10);
DLIB_TEST(get_pixel_intensity(p_rgb) == 0);
DLIB_TEST(get_pixel_intensity(p_gray16) == 0);
p_float = 54.25;
DLIB_TEST(get_pixel_intensity(p_float) == 54.25);
assign_pixel(p_gray, p_float);
DLIB_TEST(get_pixel_intensity(p_gray) == 54);
assign_pixel_intensity(p_float, -1000);
assign_pixel_intensity(p_schar, -100);
assign_pixel_intensity(p_int, -10000);
assign_pixel_intensity(p_gray, -100);
DLIB_TEST(p_float == -1000);
DLIB_TEST(get_pixel_intensity(p_float) == -1000);
DLIB_TEST(p_schar == -100);
DLIB_TEST(get_pixel_intensity(p_schar) == -100);
DLIB_TEST(p_int == -10000);
DLIB_TEST(get_pixel_intensity(p_int) == -10000);
DLIB_TEST(p_gray == 0);
assign_pixel_intensity(p_gray, 1000);
DLIB_TEST(p_gray == 255);
DLIB_TEST(get_pixel_intensity(p_gray) == 255);
assign_pixel_intensity(p_float, p_gray);
DLIB_TEST(p_float == 255);
DLIB_TEST(get_pixel_intensity(p_float) == 255);
assign_pixel_intensity(p_int, p_gray);
DLIB_TEST(p_int == 255);
DLIB_TEST(get_pixel_intensity(p_int) == 255);
p_float = 1e10;
assign_pixel(p_schar, p_float);
DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
p_float = -1e10;
assign_pixel(p_schar, p_float);
DLIB_TEST(p_schar == std::numeric_limits<signed char>::min());
double p_double = 1e200;
assign_pixel(p_float, p_double);
DLIB_TEST(p_float == std::numeric_limits<float>::max());
p_double = -1e200;
assign_pixel(p_float, p_double);
DLIB_TEST(p_float == -std::numeric_limits<float>::max());
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment