Add stride to PixelData
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PixelData.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali-test-suite-utils.h>
19 #include <dali/public-api/common/dali-vector.h>
20 #include <dali/public-api/images/pixel-data.h>
21 #include <dali/public-api/images/pixel.h>
22
23 #include <cstdlib>
24
25 using namespace Dali;
26
27 int UtcDaliPixelData01(void)
28 {
29   TestApplication application;
30
31   unsigned int width      = 10u;
32   unsigned int height     = 10u;
33   unsigned int bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::RGB888);
34
35   unsigned char* buffer    = reinterpret_cast<unsigned char*>(malloc(bufferSize));
36   PixelData      pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE);
37
38   DALI_TEST_CHECK(pixelData);
39   DALI_TEST_CHECK(pixelData.GetWidth() == width);
40   DALI_TEST_CHECK(pixelData.GetHeight() == height);
41   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::RGB888);
42
43   END_TEST;
44 }
45
46 int UtcDaliPixelData02(void)
47 {
48   TestApplication application;
49
50   unsigned int   width      = 10u;
51   unsigned int   height     = 10u;
52   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
53   unsigned char* buffer     = new unsigned char[bufferSize];
54   buffer[0]                 = 'a';
55
56   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
57
58   DALI_TEST_CHECK(pixelData);
59   DALI_TEST_CHECK(pixelData.GetWidth() == width);
60   DALI_TEST_CHECK(pixelData.GetHeight() == height);
61   DALI_TEST_CHECK(pixelData.GetStride() == 0);
62   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
63
64   END_TEST;
65 }
66
67 int UtcDaliPixelData03(void)
68 {
69   TestApplication application;
70
71   uint32_t width      = 10u;
72   uint32_t height     = 10u;
73   uint32_t stride     = 12u;
74   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::RGB888);
75
76   uint8_t*  buffer    = reinterpret_cast<uint8_t*>(malloc(bufferSize));
77   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::RGB888, PixelData::FREE);
78
79   DALI_TEST_CHECK(pixelData);
80   DALI_TEST_CHECK(pixelData.GetWidth() == width);
81   DALI_TEST_CHECK(pixelData.GetHeight() == height);
82   DALI_TEST_CHECK(pixelData.GetStride() == stride);
83   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::RGB888);
84
85   END_TEST;
86 }
87
88 int UtcDaliPixelData04(void)
89 {
90   TestApplication application;
91
92   uint32_t width      = 10u;
93   uint32_t height     = 10u;
94   uint32_t stride     = 12u;
95   uint32_t bufferSize = stride * height * Pixel::GetBytesPerPixel(Pixel::L8);
96   uint8_t* buffer     = new uint8_t[bufferSize];
97   buffer[0]           = 'a';
98
99   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, stride, Pixel::L8, PixelData::DELETE_ARRAY);
100
101   DALI_TEST_CHECK(pixelData);
102   DALI_TEST_CHECK(pixelData.GetWidth() == width);
103   DALI_TEST_CHECK(pixelData.GetHeight() == height);
104   DALI_TEST_CHECK(pixelData.GetStride() == stride);
105   DALI_TEST_CHECK(pixelData.GetPixelFormat() == Pixel::L8);
106
107   END_TEST;
108 }
109
110 int UtcDaliPixelDataCopyConstructor(void)
111 {
112   TestApplication application;
113
114   unsigned int   width      = 10u;
115   unsigned int   height     = 10u;
116   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
117   unsigned char* buffer     = new unsigned char[bufferSize];
118   PixelData      pixelData  = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
119
120   PixelData pixelDataCopy(pixelData);
121
122   DALI_TEST_EQUALS((bool)pixelDataCopy, true, TEST_LOCATION);
123   END_TEST;
124 }
125
126 int UtcDaliPixelDataAssignmentOperator(void)
127 {
128   TestApplication application;
129
130   unsigned int   width      = 10u;
131   unsigned int   height     = 10u;
132   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
133   unsigned char* buffer     = new unsigned char[bufferSize];
134   PixelData      pixelData  = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
135
136   PixelData pixelData2;
137   DALI_TEST_EQUALS((bool)pixelData2, false, TEST_LOCATION);
138
139   pixelData2 = pixelData;
140   DALI_TEST_EQUALS((bool)pixelData2, true, TEST_LOCATION);
141
142   END_TEST;
143 }
144
145 int UtcDaliPixelDataMoveConstructor(void)
146 {
147   TestApplication application;
148
149   unsigned int   width      = 10u;
150   unsigned int   height     = 10u;
151   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
152   unsigned char* buffer     = new unsigned char[bufferSize];
153
154   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
155   DALI_TEST_CHECK(pixelData);
156   DALI_TEST_EQUALS(width, pixelData.GetWidth(), TEST_LOCATION);
157   DALI_TEST_EQUALS(height, pixelData.GetHeight(), TEST_LOCATION);
158
159   PixelData moved = std::move(pixelData);
160   DALI_TEST_CHECK(moved);
161   DALI_TEST_EQUALS(width, moved.GetWidth(), TEST_LOCATION);
162   DALI_TEST_EQUALS(height, moved.GetHeight(), TEST_LOCATION);
163   DALI_TEST_CHECK(!pixelData);
164
165   END_TEST;
166 }
167
168 int UtcDaliPixelDataMoveAssignment(void)
169 {
170   TestApplication application;
171
172   unsigned int   width      = 10u;
173   unsigned int   height     = 10u;
174   unsigned int   bufferSize = width * height * Pixel::GetBytesPerPixel(Pixel::L8);
175   unsigned char* buffer     = new unsigned char[bufferSize];
176
177   PixelData pixelData = PixelData::New(buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY);
178   DALI_TEST_CHECK(pixelData);
179   DALI_TEST_EQUALS(width, pixelData.GetWidth(), TEST_LOCATION);
180   DALI_TEST_EQUALS(height, pixelData.GetHeight(), TEST_LOCATION);
181
182   PixelData moved;
183   moved = std::move(pixelData);
184   DALI_TEST_CHECK(moved);
185   DALI_TEST_EQUALS(width, moved.GetWidth(), TEST_LOCATION);
186   DALI_TEST_EQUALS(height, moved.GetHeight(), TEST_LOCATION);
187   DALI_TEST_CHECK(!pixelData);
188
189   END_TEST;
190 }
191
192 int UtcDaliPixelDataGetPixelFormatNegative(void)
193 {
194   TestApplication application;
195   Dali::PixelData instance;
196   try
197   {
198     instance.GetPixelFormat();
199     DALI_TEST_CHECK(false); // Should not get here
200   }
201   catch(...)
202   {
203     DALI_TEST_CHECK(true); // We expect an assert
204   }
205   END_TEST;
206 }
207
208 int UtcDaliPixelDataGetWidthNegative(void)
209 {
210   TestApplication application;
211   Dali::PixelData instance;
212   try
213   {
214     instance.GetWidth();
215     DALI_TEST_CHECK(false); // Should not get here
216   }
217   catch(...)
218   {
219     DALI_TEST_CHECK(true); // We expect an assert
220   }
221   END_TEST;
222 }
223
224 int UtcDaliPixelDataGetHeightNegative(void)
225 {
226   TestApplication application;
227   Dali::PixelData instance;
228   try
229   {
230     instance.GetHeight();
231     DALI_TEST_CHECK(false); // Should not get here
232   }
233   catch(...)
234   {
235     DALI_TEST_CHECK(true); // We expect an assert
236   }
237   END_TEST;
238 }