1 // Copyright (C) 2007  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 
4 
5 #include <sstream>
6 #include <string>
7 #include <cstdlib>
8 #include <ctime>
9 #include <dlib/pixel.h>
10 #include <dlib/matrix.h>
11 #include <dlib/image_io.h>
12 
13 #include "tester.h"
14 
15 namespace
16 {
17     using namespace test;
18     using namespace dlib;
19     using namespace std;
20 
21     logger dlog("test.pixel");
22 
23 
pixel_test()24     void pixel_test (
25     )
26     /*!
27         ensures
28             - runs tests on pixel objects and functions for compliance with the specs
29     !*/
30     {
31 
32         print_spinner();
33 
34         unsigned char p_gray;
35         unsigned short p_gray16;
36         long p_int;
37         float p_float;
38         signed char p_schar;
39         rgb_pixel p_rgb,p_rgb2;
40         hsi_pixel p_hsi, p_hsi2;
41         rgb_alpha_pixel p_rgba;
42         lab_pixel p_lab, p_lab2;
43 
44         assign_pixel(p_int, 0.0f);
45         assign_pixel(p_float, 0.0f);
46         assign_pixel(p_schar, 0);
47 
48         assign_pixel(p_gray, -2);
49         assign_pixel(p_rgb,0);
50         assign_pixel(p_hsi, -4);
51         assign_pixel(p_rgba, p_int);
52         assign_pixel(p_gray16,0);
53         assign_pixel(p_lab,-400);
54 
55         DLIB_TEST(p_int == 0);
56         DLIB_TEST(p_float == 0);
57         DLIB_TEST(p_schar == 0);
58 
59         DLIB_TEST(p_gray == 0);
60         DLIB_TEST(p_gray16 == 0);
61 
62         DLIB_TEST(p_rgb.red == 0);
63         DLIB_TEST(p_rgb.green == 0);
64         DLIB_TEST(p_rgb.blue == 0);
65 
66         DLIB_TEST(p_rgba.red == 0);
67         DLIB_TEST(p_rgba.green == 0);
68         DLIB_TEST(p_rgba.blue == 0);
69         DLIB_TEST(p_rgba.alpha == 255);
70 
71         DLIB_TEST(p_hsi.h == 0);
72         DLIB_TEST(p_hsi.s == 0);
73         DLIB_TEST(p_hsi.i == 0);
74 
75         DLIB_TEST(p_lab.l == 0);
76         DLIB_TEST(p_lab.a == 128);
77         DLIB_TEST(p_lab.b == 128);
78 
79         assign_pixel(p_gray,10);
80         assign_pixel(p_gray16,10);
81         assign_pixel(p_rgb,10);
82         assign_pixel(p_hsi,10);
83         assign_pixel(p_rgba,10);
84         assign_pixel(p_lab,10);
85 
86         assign_pixel(p_int, -10);
87         assign_pixel(p_float, -10);
88         assign_pixel(p_schar, -10);
89 
90         DLIB_TEST(p_int == -10);
91         DLIB_TEST(p_float == -10);
92         DLIB_TEST(p_schar == -10);
93 
94         DLIB_TEST(p_gray == 10);
95         DLIB_TEST(p_gray16 == 10);
96 
97         DLIB_TEST(p_rgb.red == 10);
98         DLIB_TEST(p_rgb.green == 10);
99         DLIB_TEST(p_rgb.blue == 10);
100 
101         DLIB_TEST(p_rgba.red == 10);
102         DLIB_TEST(p_rgba.green == 10);
103         DLIB_TEST(p_rgba.blue == 10);
104         DLIB_TEST(p_rgba.alpha == 255);
105 
106         DLIB_TEST(p_hsi.h == 0);
107         DLIB_TEST(p_hsi.s == 0);
108         DLIB_TEST(p_hsi.i == 10);
109 
110         DLIB_TEST(p_lab.l == 10);
111         DLIB_TEST(p_lab.a == 128);
112         DLIB_TEST(p_lab.b == 128);
113 
114         assign_pixel(p_gray16,12345);
115         DLIB_TEST(p_gray16 == 12345);
116 
117         assign_pixel(p_float,3.141);
118         DLIB_TEST(p_float == 3.141f);
119 
120         p_rgb.red = 255;
121         p_rgb.green = 100;
122         p_rgb.blue = 50;
123 
124         p_rgba.alpha = 4;
125         assign_pixel(p_gray,p_rgb);
126         assign_pixel(p_rgb,p_rgb);
127         assign_pixel(p_rgba,p_rgb);
128         assign_pixel(p_hsi,p_rgb);
129         assign_pixel(p_lab,p_rgb);
130 
131         assign_pixel(p_float,p_rgb);
132         assign_pixel(p_int,p_rgb);
133         assign_pixel(p_schar,p_rgb);
134 
135         DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
136 
137         DLIB_TEST(p_int == (255+100+50)/3);
138         DLIB_TEST_MSG(p_float == (255+100+50)/3, p_float - (255+100+50)/3);
139         DLIB_TEST(p_gray == (255+100+50)/3);
140 
141         DLIB_TEST(p_rgb.red == 255);
142         DLIB_TEST(p_rgb.green == 100);
143         DLIB_TEST(p_rgb.blue == 50);
144 
145         DLIB_TEST(p_rgba.red == 255);
146         DLIB_TEST(p_rgba.green == 100);
147         DLIB_TEST(p_rgba.blue == 50);
148         DLIB_TEST(p_rgba.alpha == 255);
149 
150         DLIB_TEST(p_hsi.i > 0);
151         DLIB_TEST(p_hsi.s > 0);
152         DLIB_TEST(p_hsi.h > 0);
153 
154         DLIB_TEST(p_lab.l > 0);
155         DLIB_TEST(p_lab.a > 0);
156         DLIB_TEST(p_lab.b > 0);
157 
158         assign_pixel(p_rgb,0);
159         DLIB_TEST(p_rgb.red == 0);
160         DLIB_TEST(p_rgb.green == 0);
161         DLIB_TEST(p_rgb.blue == 0);
162         assign_pixel(p_rgb, p_hsi);
163 
164         DLIB_TEST_MSG(p_rgb.red > 251 ,(int)p_rgb.green);
165         DLIB_TEST_MSG(p_rgb.green > 96 && p_rgb.green < 104,(int)p_rgb.green);
166         DLIB_TEST_MSG(p_rgb.blue > 47 && p_rgb.blue < 53,(int)p_rgb.green);
167 
168         assign_pixel(p_rgb,0);
169         DLIB_TEST(p_rgb.red == 0);
170         DLIB_TEST(p_rgb.green == 0);
171         DLIB_TEST(p_rgb.blue == 0);
172 
173         assign_pixel(p_rgb, p_lab);
174         DLIB_TEST_MSG(p_rgb.red > 251 ,(int)p_rgb.green);
175         DLIB_TEST_MSG(p_rgb.green > 96 && p_rgb.green < 104,(int)p_rgb.green);
176         DLIB_TEST_MSG(p_rgb.blue > 47 && p_rgb.blue < 53,(int)p_rgb.green);
177 
178         assign_pixel(p_hsi2, p_hsi);
179         DLIB_TEST(p_hsi.h == p_hsi2.h);
180         DLIB_TEST(p_hsi.s == p_hsi2.s);
181         DLIB_TEST(p_hsi.i == p_hsi2.i);
182         assign_pixel(p_hsi,0);
183         DLIB_TEST(p_hsi.h == 0);
184         DLIB_TEST(p_hsi.s == 0);
185         DLIB_TEST(p_hsi.i == 0);
186         assign_pixel(p_hsi, p_rgba);
187 
188         DLIB_TEST(p_hsi.h == p_hsi2.h);
189         DLIB_TEST(p_hsi.s == p_hsi2.s);
190         DLIB_TEST(p_hsi.i == p_hsi2.i);
191 
192         assign_pixel(p_lab2, p_lab);
193         DLIB_TEST(p_lab.l == p_lab2.l);
194         DLIB_TEST(p_lab.a == p_lab2.a);
195         DLIB_TEST(p_lab.b == p_lab2.b);
196         assign_pixel(p_lab,0);
197         DLIB_TEST(p_lab.l == 0);
198         DLIB_TEST(p_lab.a == 128);
199         DLIB_TEST(p_lab.b == 128);
200         assign_pixel(p_lab, p_rgba);
201 
202         DLIB_TEST(p_lab.l == p_lab2.l);
203         DLIB_TEST(p_lab.a == p_lab2.a);
204         DLIB_TEST(p_lab.b == p_lab2.b);
205 
206         assign_pixel(p_rgba, 100);
207         assign_pixel(p_gray, 10);
208         assign_pixel(p_rgb, 10);
209         assign_pixel(p_hsi, 10);
210 
211         assign_pixel(p_schar, 10);
212         assign_pixel(p_float, 10);
213         assign_pixel(p_int, 10);
214 
215         p_rgba.alpha = 0;
216         assign_pixel(p_gray, p_rgba);
217         DLIB_TEST(p_gray == 10);
218         assign_pixel(p_schar, p_rgba);
219         DLIB_TEST(p_schar == 10);
220         assign_pixel(p_int, p_rgba);
221         DLIB_TEST(p_int == 10);
222         assign_pixel(p_float, p_rgba);
223         DLIB_TEST(p_float == 10);
224         assign_pixel(p_rgb, p_rgba);
225         DLIB_TEST(p_rgb.red == 10);
226         DLIB_TEST(p_rgb.green == 10);
227         DLIB_TEST(p_rgb.blue == 10);
228 
229         assign_pixel(p_hsi, p_rgba);
230         assign_pixel(p_hsi2, p_rgb);
231         DLIB_TEST(p_hsi.h == 0);
232         DLIB_TEST(p_hsi.s == 0);
233         DLIB_TEST_MSG(p_hsi.i < p_hsi2.i+2 && p_hsi.i > p_hsi2.i -2,(int)p_hsi.i << "   " << (int)p_hsi2.i);
234 
235         // this value corresponds to RGB(10,10,10)
236         p_lab.l = 7;
237         p_lab.a = 128;
238         p_lab.b = 128;
239 
240         assign_pixel(p_lab, p_rgba);
241         assign_pixel(p_lab2, p_rgb);
242         DLIB_TEST(p_lab.a == 128);
243         DLIB_TEST(p_lab.b == 128);
244         DLIB_TEST_MSG(p_lab.l < p_lab2.l+2 && p_lab.l > p_lab2.l -2,(int)p_lab.l << "   " << (int)p_lab2.l);
245 
246         assign_pixel(p_lab, 128);
247         DLIB_TEST(p_lab.l == 128);
248         DLIB_TEST(p_lab.a == 128);
249         DLIB_TEST(p_lab.b == 128);
250         assign_pixel(p_rgb, p_lab);
251         //Lab midpoint (50,0,0) is not same as RGB midpoint (127,127,127)
252         DLIB_TEST(p_rgb.red == 119);
253         DLIB_TEST(p_rgb.green == 119);
254         DLIB_TEST(p_rgb.blue == 119);
255 
256         //Lab limit values test
257         //red, green, blue, yellow, black, white
258         p_lab.l = 84;
259         p_lab.a = 164;
260         p_lab.b = 56;
261         assign_pixel(p_rgb, p_lab);
262         DLIB_TEST(p_rgb.red == 0);
263         DLIB_TEST(p_rgb.green == 64);
264         DLIB_TEST(p_rgb.blue == 194);
265 
266         p_lab.l = 255;
267         p_lab.a = 0;
268         p_lab.b = 0;
269         assign_pixel(p_rgb, p_lab);
270         DLIB_TEST(p_rgb.red == 0);
271         DLIB_TEST(p_rgb.green == 255);
272         DLIB_TEST(p_rgb.blue == 255);
273 
274         p_lab.l = 0;
275         p_lab.a = 255;
276         p_lab.b = 0;
277 
278         assign_pixel(p_rgb, p_lab);
279         DLIB_TEST(p_rgb.red == 0);
280         DLIB_TEST(p_rgb.green == 0);
281         DLIB_TEST(p_rgb.blue == 195);
282 
283         p_lab.l = 0;
284         p_lab.a = 0;
285         p_lab.b = 255;
286 
287         assign_pixel(p_rgb, p_lab);
288         DLIB_TEST(p_rgb.red == 0);
289         DLIB_TEST(p_rgb.green == 45);
290         DLIB_TEST(p_rgb.blue == 0);
291 
292         p_lab.l = 255;
293         p_lab.a = 255;
294         p_lab.b = 0;
295         assign_pixel(p_rgb, p_lab);
296         DLIB_TEST(p_rgb.red == 255);
297         DLIB_TEST(p_rgb.green == 139);
298         DLIB_TEST(p_rgb.blue == 255);
299 
300         p_lab.l = 0;
301         p_lab.a = 255;
302         p_lab.b = 255;
303         assign_pixel(p_rgb, p_lab);
304         DLIB_TEST(p_rgb.red == 132);
305         DLIB_TEST(p_rgb.green == 0);
306         DLIB_TEST(p_rgb.blue == 0);
307 
308         p_lab.l = 255;
309         p_lab.a = 0;
310         p_lab.b = 255;
311         assign_pixel(p_rgb, p_lab);
312         DLIB_TEST(p_rgb.red == 0);
313         DLIB_TEST(p_rgb.green == 255);
314         DLIB_TEST(p_rgb.blue == 0);
315 
316         p_lab.l = 255;
317         p_lab.a = 255;
318         p_lab.b = 255;
319         assign_pixel(p_rgb, p_lab);
320         DLIB_TEST(p_rgb.red == 255);
321         DLIB_TEST(p_rgb.green == 70);
322         DLIB_TEST(p_rgb.blue == 0);
323 
324         //RGB limit tests
325         p_rgb.red = 0;
326         p_rgb.green = 0;
327         p_rgb.blue = 0;
328         assign_pixel(p_lab, p_rgb);
329         assign_pixel(p_rgb2, p_lab);
330         DLIB_TEST(p_rgb2.red < 3);
331         DLIB_TEST(p_rgb2.green < 3);
332         DLIB_TEST(p_rgb2.blue < 3);
333 
334         p_rgb.red = 255;
335         p_rgb.green = 0;
336         p_rgb.blue = 0;
337         assign_pixel(p_lab, p_rgb);
338         assign_pixel(p_rgb2, p_lab);
339         DLIB_TEST(p_rgb2.red > 252);
340         DLIB_TEST(p_rgb2.green < 3);
341         DLIB_TEST(p_rgb2.blue < 3);
342 
343         p_rgb.red = 0;
344         p_rgb.green = 255;
345         p_rgb.blue = 0;
346         assign_pixel(p_lab, p_rgb);
347         assign_pixel(p_rgb2, p_lab);
348         DLIB_TEST(p_rgb2.red < 8);
349         DLIB_TEST(p_rgb2.green > 252);
350         DLIB_TEST(p_rgb2.blue < 5);
351 
352         p_rgb.red = 0;
353         p_rgb.green = 0;
354         p_rgb.blue = 255;
355         assign_pixel(p_lab, p_rgb);
356         assign_pixel(p_rgb2, p_lab);
357         DLIB_TEST(p_rgb2.red < 3);
358         DLIB_TEST(p_rgb2.green < 3);
359         DLIB_TEST(p_rgb2.blue > 252);
360 
361         p_rgb.red = 255;
362         p_rgb.green = 255;
363         p_rgb.blue = 0;
364         assign_pixel(p_lab, p_rgb);
365         assign_pixel(p_rgb2, p_lab);
366         DLIB_TEST(p_rgb2.red > 252);
367         DLIB_TEST(p_rgb2.green > 252);
368         DLIB_TEST(p_rgb2.blue < 9);
369 
370         p_rgb.red = 0;
371         p_rgb.green = 255;
372         p_rgb.blue = 255;
373         assign_pixel(p_lab, p_rgb);
374         assign_pixel(p_rgb2, p_lab);
375         DLIB_TEST(p_rgb2.red < 5);
376         DLIB_TEST(p_rgb2.green > 252);
377         DLIB_TEST(p_rgb2.blue > 252);
378 
379         p_rgb.red = 255;
380         p_rgb.green = 0;
381         p_rgb.blue = 255;
382         assign_pixel(p_lab, p_rgb);
383         assign_pixel(p_rgb2, p_lab);
384         DLIB_TEST(p_rgb2.red> 252);
385         DLIB_TEST(p_rgb2.green < 6);
386         DLIB_TEST(p_rgb2.blue > 252);
387 
388         p_rgb.red = 255;
389         p_rgb.green = 255;
390         p_rgb.blue = 255;
391         assign_pixel(p_lab, p_rgb);
392         assign_pixel(p_rgb2, p_lab);
393         DLIB_TEST(p_rgb2.red > 252 );
394         DLIB_TEST(p_rgb2.green> 252);
395         DLIB_TEST(p_rgb2.blue > 252);
396 
397 
398         assign_pixel(p_rgba, 100);
399         assign_pixel(p_gray, 10);
400         assign_pixel(p_schar, 10);
401         assign_pixel(p_float, 10);
402         assign_pixel(p_int, 10);
403 
404         assign_pixel(p_rgb, 10);
405         p_rgba.alpha = 128;
406         assign_pixel(p_gray, p_rgba);
407         assign_pixel(p_schar, p_rgba);
408         assign_pixel(p_float, p_rgba);
409         assign_pixel(p_int, p_rgba);
410         assign_pixel(p_rgb, p_rgba);
411         DLIB_TEST(p_gray == (100 + 10)/2);
412         DLIB_TEST(p_schar == (100 + 10)/2);
413         DLIB_TEST(p_int == (100 + 10)/2);
414         DLIB_TEST(p_float == (100 + 10)/2);
415         DLIB_TEST(p_rgb.red == (100 + 10)/2);
416         DLIB_TEST(p_rgb.green == (100 + 10)/2);
417         DLIB_TEST(p_rgb.blue == (100 + 10)/2);
418 
419         assign_pixel(p_rgba, 100);
420         assign_pixel(p_gray, 10);
421         assign_pixel(p_schar, 10);
422         assign_pixel(p_int, 10);
423         assign_pixel(p_float, 10);
424         assign_pixel(p_rgb, 10);
425         DLIB_TEST(p_rgba.alpha == 255);
426         assign_pixel(p_gray, p_rgba);
427         assign_pixel(p_schar, p_rgba);
428         assign_pixel(p_int, p_rgba);
429         assign_pixel(p_float, p_rgba);
430         assign_pixel(p_rgb, p_rgba);
431         DLIB_TEST(p_gray == 100);
432         DLIB_TEST(p_schar == 100);
433         DLIB_TEST(p_int == 100);
434         DLIB_TEST(p_float == 100);
435         DLIB_TEST(p_rgb.red == 100);
436         DLIB_TEST(p_rgb.green == 100);
437         DLIB_TEST(p_rgb.blue == 100);
438 
439 
440         p_rgb.red = 1;
441         p_rgb.green = 2;
442         p_rgb.blue = 3;
443 
444         p_rgba.red = 4;
445         p_rgba.green = 5;
446         p_rgba.blue = 6;
447         p_rgba.alpha = 7;
448 
449         p_gray = 8;
450         p_schar = 9;
451         p_int = 10;
452         p_float = 8.5;
453 
454         p_hsi.h = 9;
455         p_hsi.s = 10;
456         p_hsi.i = 11;
457 
458         p_lab.l = 10;
459         p_lab.a = 9;
460         p_lab.b = 8;
461 
462         ostringstream sout;
463         serialize(p_rgb,sout);
464         serialize(p_rgba,sout);
465         serialize(p_gray,sout);
466         serialize(p_schar,sout);
467         serialize(p_int,sout);
468         serialize(p_float,sout);
469         serialize(p_hsi,sout);
470         serialize(p_lab,sout);
471 
472         assign_pixel(p_rgb,0);
473         assign_pixel(p_rgba,0);
474         assign_pixel(p_gray,0);
475         assign_pixel(p_schar,0);
476         assign_pixel(p_int,0);
477         assign_pixel(p_float,0);
478         assign_pixel(p_hsi,0);
479         assign_pixel(p_lab,0);
480 
481         istringstream sin(sout.str());
482 
483         deserialize(p_rgb,sin);
484         deserialize(p_rgba,sin);
485         deserialize(p_gray,sin);
486         deserialize(p_schar,sin);
487         deserialize(p_int,sin);
488         deserialize(p_float,sin);
489         deserialize(p_hsi,sin);
490         deserialize(p_lab,sin);
491 
492         DLIB_TEST(p_rgb.red == 1);
493         DLIB_TEST(p_rgb.green == 2);
494         DLIB_TEST(p_rgb.blue == 3);
495 
496         DLIB_TEST(p_rgba.red == 4);
497         DLIB_TEST(p_rgba.green == 5);
498         DLIB_TEST(p_rgba.blue == 6);
499         DLIB_TEST(p_rgba.alpha == 7);
500 
501         DLIB_TEST(p_gray == 8);
502         DLIB_TEST(p_schar == 9);
503         DLIB_TEST(p_int == 10);
504         DLIB_TEST(p_float == 8.5);
505 
506         DLIB_TEST(p_hsi.h == 9);
507         DLIB_TEST(p_hsi.s == 10);
508         DLIB_TEST(p_hsi.i == 11);
509 
510         DLIB_TEST(p_lab.l == 10);
511         DLIB_TEST(p_lab.a == 9);
512         DLIB_TEST(p_lab.b == 8);
513 
514         {
515             matrix<double,1,1> m_gray, m_schar, m_int, m_float;
516             matrix<double,3,1> m_rgb, m_hsi, m_lab;
517 
518             m_gray = pixel_to_vector<double>(p_gray);
519             m_schar = pixel_to_vector<double>(p_schar);
520             m_int = pixel_to_vector<double>(p_int);
521             m_float = pixel_to_vector<double>(p_float);
522 
523             m_hsi = pixel_to_vector<double>(p_hsi);
524             m_rgb = pixel_to_vector<double>(p_rgb);
525             m_lab = pixel_to_vector<double>(p_lab);
526 
527             DLIB_TEST(m_gray(0) == p_gray);
528             DLIB_TEST(m_float(0) == p_float);
529             DLIB_TEST(m_int(0) == p_int);
530             DLIB_TEST(m_schar(0) == p_schar);
531 
532             DLIB_TEST(m_rgb(0) == p_rgb.red);
533             DLIB_TEST(m_rgb(1) == p_rgb.green);
534             DLIB_TEST(m_rgb(2) == p_rgb.blue);
535             DLIB_TEST(m_hsi(0) == p_hsi.h);
536             DLIB_TEST(m_hsi(1) == p_hsi.s);
537             DLIB_TEST(m_hsi(2) == p_hsi.i);
538             DLIB_TEST(m_lab(0) == p_lab.l);
539             DLIB_TEST(m_lab(1) == p_lab.a);
540             DLIB_TEST(m_lab(2) == p_lab.b);
541 
542             DLIB_TEST(p_rgb.red == 1);
543             DLIB_TEST(p_rgb.green == 2);
544             DLIB_TEST(p_rgb.blue == 3);
545 
546             DLIB_TEST(p_rgba.red == 4);
547             DLIB_TEST(p_rgba.green == 5);
548             DLIB_TEST(p_rgba.blue == 6);
549             DLIB_TEST(p_rgba.alpha == 7);
550 
551             DLIB_TEST(p_gray == 8);
552             DLIB_TEST(p_int == 10);
553             DLIB_TEST(p_float == 8.5);
554             DLIB_TEST(p_schar == 9);
555 
556             DLIB_TEST(p_hsi.h == 9);
557             DLIB_TEST(p_hsi.s == 10);
558             DLIB_TEST(p_hsi.i == 11);
559 
560             DLIB_TEST(p_lab.l == 10);
561             DLIB_TEST(p_lab.a == 9);
562             DLIB_TEST(p_lab.b == 8);
563 
564             assign_pixel(p_gray,0);
565             assign_pixel(p_hsi,0);
566             assign_pixel(p_rgb,0);
567             assign_pixel(p_lab,0);
568 
569             vector_to_pixel(p_float, m_float);
570             vector_to_pixel(p_gray, m_gray);
571             vector_to_pixel(p_hsi, m_hsi);
572             vector_to_pixel(p_rgb, m_rgb);
573             vector_to_pixel(p_lab, m_lab);
574 
575             DLIB_TEST(p_rgb.red == 1);
576             DLIB_TEST(p_rgb.green == 2);
577             DLIB_TEST(p_rgb.blue == 3);
578 
579             DLIB_TEST(p_rgba.red == 4);
580             DLIB_TEST(p_rgba.green == 5);
581             DLIB_TEST(p_rgba.blue == 6);
582             DLIB_TEST(p_rgba.alpha == 7);
583 
584             DLIB_TEST(p_gray == 8);
585             DLIB_TEST(p_float == 8.5);
586 
587             DLIB_TEST(p_hsi.h == 9);
588             DLIB_TEST(p_hsi.s == 10);
589             DLIB_TEST(p_hsi.i == 11);
590 
591             DLIB_TEST(p_lab.l == 10);
592             DLIB_TEST(p_lab.a == 9);
593             DLIB_TEST(p_lab.b == 8);
594         }
595 
596 
597 
598 
599         {
600             unsigned char p_gray;
601             unsigned short p_gray16;
602             long p_int;
603             float p_float;
604             signed char p_schar;
605             rgb_pixel p_rgb;
606             hsi_pixel p_hsi, p_hsi2;
607             rgb_alpha_pixel p_rgba;
608             lab_pixel p_lab;
609 
610 
611             assign_pixel(p_gray, 0);
612             assign_pixel(p_gray16, 0);
613             assign_pixel(p_int, 0);
614             assign_pixel(p_float, 0);
615             assign_pixel(p_schar, 0);
616             assign_pixel(p_rgb, 0);
617             assign_pixel(p_hsi, 0);
618             assign_pixel(p_lab, 0);
619 
620 
621             assign_pixel(p_gray, 100);
622             assign_pixel(p_schar, p_gray);
623             DLIB_TEST(p_schar == 100);
624 
625             assign_pixel(p_gray, 200);
626             assign_pixel(p_schar, p_gray);
627             DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
628 
629             assign_pixel(p_int, p_gray);
630             DLIB_TEST(p_int == 200);
631 
632             assign_pixel(p_float, p_gray);
633             DLIB_TEST(p_float == 200);
634 
635             assign_pixel(p_rgb, p_float);
636             DLIB_TEST(p_rgb.red == 200);
637             DLIB_TEST(p_rgb.green == 200);
638             DLIB_TEST(p_rgb.blue == 200);
639 
640             p_schar = 0;
641             assign_pixel(p_schar, p_rgb);
642             DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
643 
644 
645             p_schar = -10;
646             assign_pixel(p_float, p_schar);
647             DLIB_TEST(p_float == -10);
648             assign_pixel(p_int, p_schar);
649             DLIB_TEST(p_int == -10);
650             assign_pixel(p_schar, p_schar);
651             DLIB_TEST(p_schar == -10);
652             assign_pixel(p_gray, p_schar);
653             DLIB_TEST(p_gray == 0);
654 
655             assign_pixel(p_rgb, p_schar);
656             DLIB_TEST(p_rgb.red == 0);
657             DLIB_TEST(p_rgb.green == 0);
658             DLIB_TEST(p_rgb.blue == 0);
659 
660             assign_pixel(p_gray16, p_schar);
661             DLIB_TEST(p_gray16 == 0);
662 
663             DLIB_TEST(get_pixel_intensity(p_float) == -10);
664             DLIB_TEST(get_pixel_intensity(p_int) == -10);
665             DLIB_TEST(get_pixel_intensity(p_schar) == -10);
666             DLIB_TEST(get_pixel_intensity(p_rgb) == 0);
667             DLIB_TEST(get_pixel_intensity(p_gray16) == 0);
668 
669             p_rgb.red = 100;
670             p_rgb.green = 100;
671             p_rgb.blue = 100;
672             DLIB_TEST(get_pixel_intensity(p_rgb) == 100);
673             p_rgb.red = 1;
674             p_rgb.green = 2;
675             p_rgb.blue = 3;
676             DLIB_TEST(get_pixel_intensity(p_rgb) == 2);
677             p_rgba.alpha = 100;
678             p_rgba.red = 100;
679             p_rgba.green = 100;
680             p_rgba.blue = 100;
681             DLIB_TEST(get_pixel_intensity(p_rgba) == 100);
682             p_rgba.red = 1;
683             p_rgba.green = 2;
684             p_rgba.blue = 3;
685             p_rgba.alpha = 0;
686             DLIB_TEST(get_pixel_intensity(p_rgba) == 2);
687             p_hsi.h = 123;
688             p_hsi.s = 100;
689             p_hsi.i = 84;
690             DLIB_TEST(get_pixel_intensity(p_hsi) == 84);
691 
692             p_lab.l = 123;
693             p_lab.a = 100;
694             p_lab.b = 84;
695             DLIB_TEST(get_pixel_intensity(p_lab) == 123);
696 
697             p_float = 54.25;
698             DLIB_TEST(get_pixel_intensity(p_float) == 54.25);
699 
700             assign_pixel(p_gray, p_float);
701             DLIB_TEST(get_pixel_intensity(p_gray) == 54);
702 
703             assign_pixel_intensity(p_float, -1000);
704             assign_pixel_intensity(p_schar, -100);
705             assign_pixel_intensity(p_int, -10000);
706             assign_pixel_intensity(p_gray, -100);
707 
708             p_rgba.red = 10;
709             p_rgba.green = 10;
710             p_rgba.blue = 10;
711             p_rgba.alpha = 0;
712             DLIB_TEST_MSG(get_pixel_intensity(p_rgba) == 10, (int)get_pixel_intensity(p_rgba));
713             assign_pixel_intensity(p_rgba, 2);
714             DLIB_TEST_MSG(p_rgba.red == 2, (int)p_rgba.red);
715             DLIB_TEST_MSG(p_rgba.green == 2, (int)p_rgba.green);
716             DLIB_TEST_MSG(p_rgba.blue == 2, (int)p_rgba.blue);
717             DLIB_TEST_MSG(p_rgba.alpha == 0, (int)p_rgba.alpha);
718             DLIB_TEST_MSG(get_pixel_intensity(p_rgba) == 2, (int)get_pixel_intensity(p_rgba));
719 
720             DLIB_TEST(p_float == -1000);
721             DLIB_TEST(get_pixel_intensity(p_float) == -1000);
722             DLIB_TEST(p_schar == -100);
723             DLIB_TEST(get_pixel_intensity(p_schar) == -100);
724             DLIB_TEST(p_int == -10000);
725             DLIB_TEST(get_pixel_intensity(p_int) == -10000);
726             DLIB_TEST(p_gray == 0);
727             assign_pixel_intensity(p_gray, 1000);
728             DLIB_TEST(p_gray == 255);
729             DLIB_TEST(get_pixel_intensity(p_gray) == 255);
730 
731             assign_pixel_intensity(p_float, p_gray);
732             DLIB_TEST(p_float == 255);
733             DLIB_TEST(get_pixel_intensity(p_float) == 255);
734 
735             assign_pixel_intensity(p_int, p_gray);
736             DLIB_TEST(p_int == 255);
737             DLIB_TEST(get_pixel_intensity(p_int) == 255);
738 
739 
740             p_float = 1e10;
741             assign_pixel(p_schar, p_float);
742             DLIB_TEST(p_schar == std::numeric_limits<signed char>::max());
743 
744             p_float = -1e10;
745             assign_pixel(p_schar, p_float);
746             DLIB_TEST(p_schar == std::numeric_limits<signed char>::min());
747 
748             double p_double = 1e200;
749             assign_pixel(p_float, p_double);
750             DLIB_TEST(p_float == std::numeric_limits<float>::max());
751 
752             p_double = -1e200;
753             assign_pixel(p_float, p_double);
754             DLIB_TEST(p_float == -std::numeric_limits<float>::max());
755         }
756 
757 
758     }
759 
760 
761 
762 
763     class pixel_tester : public tester
764     {
765     public:
pixel_tester()766         pixel_tester (
767         ) :
768             tester ("test_pixel",
769                     "Runs tests on the pixel objects and functions.")
770         {}
771 
perform_test()772         void perform_test (
773         )
774         {
775             pixel_test();
776         }
777     } a;
778 
779 }
780