Commit c2ed90ec authored by Davis King's avatar Davis King
Browse files

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;
}; };
......
This diff is collapsed.
...@@ -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