Formatting automated-tests
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-utils / dali-test-img-utils.h
1 #ifndef DALI_TEST_IMG_UTILS_H
2 #define DALI_TEST_IMG_UTILS_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstdarg>
23 #include <cstdio>
24 #include <cstring>
25 #include <iostream>
26
27 // INTERNAL INCLUDES
28 #include <dali/dali.h>
29 #include <dali/devel-api/adaptor-framework/image-loading.h>
30 #include <dali/public-api/dali-core.h>
31
32 #include <dali-test-suite-utils.h>
33
34 using namespace Dali;
35
36 namespace
37 {
38 /**
39  * Test whether two buffers are equal with tolerance value.
40  * @param[in] buffer1 The first buffer
41  * @param[in] buffer2 The second pixelbuffer
42  * @param[in] tolerance value, maximum difference to accept the similarity of buffers.
43  * @param[in] location The TEST_LOCATION macro should be used here
44  */
45
46 inline void DALI_TEST_EQUALS(const unsigned char* buffer1, const unsigned char* buffer2, unsigned int tolerance, long size, const char* location)
47 {
48   if(!tolerance)
49   {
50     if(memcmp(buffer1, buffer2, size))
51     {
52       fprintf(stderr, "%s, checking buffer1 == buffer2\n", location);
53       tet_result(TET_FAIL);
54     }
55     else
56     {
57       tet_result(TET_PASS);
58     }
59   }
60   else
61   {
62     const unsigned char* buff1 = buffer1;
63     const unsigned char* buff2 = buffer2;
64     unsigned int         i     = 0;
65     //Create a mask to fast compare, it is expected to be similar values.
66     unsigned int maskBits = 0;
67     while(maskBits < tolerance)
68     {
69       maskBits |= (1 << i);
70       i++;
71     }
72     maskBits &= ~(1 << --i);
73     maskBits = ~maskBits;
74
75     bool equal = true;
76     for(i = 0; i < size; ++i, ++buff1, ++buff2)
77     {
78       //Check bit difference, if exist, do more exhaustive comparison with tolerance value
79       if((*buff1 ^ *buff2) & maskBits)
80       {
81         if(*buff1 < *buff2)
82         {
83           unsigned int diff = *buff2 - *buff1;
84           if(diff > tolerance)
85           {
86             equal = false;
87             break;
88           }
89         }
90         else
91         {
92           unsigned int diff = *buff1 - *buff2;
93           if(diff > tolerance)
94           {
95             equal = false;
96             break;
97           }
98         }
99       }
100     }
101     if(!equal)
102     {
103       fprintf(stderr, "%s, byte %d, checking %u == %u\n", location, i, *buff1, *buff2);
104       tet_result(TET_FAIL);
105     }
106     else
107     {
108       tet_result(TET_PASS);
109     }
110   }
111 }
112
113 /**
114  * Test whether two pixelbuffers are equal with tolerance value, with check of width and height.
115  * @param[in] pixelBuffer1 The first buffer
116  * @param[in] pixelBuffer2 The second pixelbuffer
117  * @param[in] tolerance value, maximum difference to accept the similarity of pixel buffers.
118  * @param[in] location The TEST_LOCATION macro should be used here
119  */
120
121 inline void DALI_IMAGE_TEST_EQUALS(Dali::Devel::PixelBuffer pixelBuffer1, Dali::Devel::PixelBuffer pixelBuffer2, unsigned int tolerance, const char* location)
122 {
123   if((pixelBuffer1.GetPixelFormat() != Pixel::RGB888) || (pixelBuffer2.GetPixelFormat() != Pixel::RGB888))
124   {
125     fprintf(stderr, "%s, PixelFormat != Pixel::RGB888, test only support Pixel::RGB888 formats\n", location);
126     tet_result(TET_FAIL);
127   }
128   else if((pixelBuffer1.GetWidth() != pixelBuffer1.GetWidth()) || (pixelBuffer1.GetHeight() != pixelBuffer1.GetHeight()))
129   {
130     fprintf(stderr, "%s, Different Image sizes\n", location);
131     tet_result(TET_FAIL);
132   }
133   else
134   {
135     DALI_TEST_EQUALS(pixelBuffer1.GetBuffer(), pixelBuffer2.GetBuffer(), tolerance, pixelBuffer1.GetHeight() * pixelBuffer1.GetWidth() * 3, location);
136   }
137 }
138
139 } // namespace
140
141 #endif // DALI_TEST_SUITE_UTILS_H