forked from smooth80/defold
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_texc.cpp
505 lines (409 loc) · 13.3 KB
/
test_texc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright 2020 The Defold Foundation
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#define JC_TEST_IMPLEMENTATION
#include <jc_test/jc_test.h>
#include <dlib/image.h>
#include <string.h> // memcmp
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>
#include "../texc.h"
#include "../texc_private.h"
class TexcTest : public jc_test_base_class
{
protected:
virtual void SetUp()
{
}
virtual void TearDown()
{
}
};
uint8_t default_data_l[4] =
{
255, 0, 0, 255
};
static dmTexc::HTexture CreateDefaultL8(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_L8, dmTexc::CS_LRGB, compression_type, default_data_l);
}
uint16_t default_data_l8a8[4] =
{
0xffff, 0xff00, 0xff00, 0xffff,
};
static dmTexc::HTexture CreateDefaultL8A8(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_L8A8, dmTexc::CS_LRGB, compression_type, default_data_l8a8);
}
uint16_t default_data_rgb_565[4] =
{
dmTexc::RGB888ToRGB565(0xff, 0, 0),
dmTexc::RGB888ToRGB565(0, 0xff, 0),
dmTexc::RGB888ToRGB565(0, 0, 0xff),
dmTexc::RGB888ToRGB565(0xff, 0xff, 0xff),
};
static dmTexc::HTexture CreateDefaultRGB16(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_R5G6B5, dmTexc::CS_LRGB, compression_type, default_data_rgb_565);
}
uint8_t default_data_rgb_888[4*3] =
{
255, 0, 0,
0, 255, 0,
0, 0, 255,
255, 255, 255
};
static dmTexc::HTexture CreateDefaultRGB24(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_R8G8B8, dmTexc::CS_LRGB, compression_type, default_data_rgb_888);
}
uint8_t default_data_rgba_8888[4*4] =
{
255, 0, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
255, 255, 255, 255
};
static dmTexc::HTexture CreateDefaultRGBA32(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_R8G8B8A8, dmTexc::CS_LRGB, compression_type, default_data_rgba_8888);
}
uint16_t default_data_rgba_4444[4] =
{
dmTexc::RGBA8888ToRGBA4444(255, 0, 0, 255),
dmTexc::RGBA8888ToRGBA4444(0, 255, 0, 255),
dmTexc::RGBA8888ToRGBA4444(0, 0, 255, 255),
dmTexc::RGBA8888ToRGBA4444(255, 255, 255, 255)
};
static dmTexc::HTexture CreateDefaultRGBA16(dmTexc::CompressionType compression_type)
{
return dmTexc::Create(2, 2, dmTexc::PF_R4G4B4A4, dmTexc::CS_LRGB, dmTexc::CT_DEFAULT, default_data_rgba_4444);
}
struct Format
{
dmTexc::HTexture (*m_CreateFn)(dmTexc::CompressionType);
uint32_t m_BytesPerPixel;
void* m_DefaultData;
dmTexc::CompressionType m_CompressionType;
dmTexc::PixelFormat m_PixelFormat;
};
Format formats[] =
{
{CreateDefaultL8, 1, default_data_l, dmTexc::CT_DEFAULT, dmTexc::PF_L8},
{CreateDefaultL8A8, 2, default_data_l8a8, dmTexc::CT_DEFAULT, dmTexc::PF_L8A8},
{CreateDefaultRGB24, 3, default_data_rgb_888, dmTexc::CT_DEFAULT, dmTexc::PF_R8G8B8},
{CreateDefaultRGBA32, 4, default_data_rgba_8888, dmTexc::CT_DEFAULT, dmTexc::PF_R8G8B8A8},
{CreateDefaultRGB16, 2, default_data_rgb_565, dmTexc::CT_DEFAULT, dmTexc::PF_R5G6B5},
{CreateDefaultRGBA16, 2, default_data_rgba_4444, dmTexc::CT_DEFAULT, dmTexc::PF_R4G4B4A4},
};
static const size_t format_count = sizeof(formats)/sizeof(Format);
TEST_F(TexcTest, Load)
{
uint8_t out[4*4];
uint8_t expected_rgba[4*4];
for (uint32_t i = 0; i < format_count ; ++i)
{
Format& format = formats[i];
dmTexc::HTexture texture = (*format.m_CreateFn)(format.m_CompressionType);
ASSERT_NE(dmTexc::INVALID_TEXTURE, texture);
dmTexc::Header header;
dmTexc::GetHeader(texture, &header);
ASSERT_EQ(2u, header.m_Width);
ASSERT_EQ(2u, header.m_Height);
uint32_t outsize = dmTexc::GetData(texture, out, sizeof(out));
// At this point, it's RGBA8888
ASSERT_EQ(header.m_Width*header.m_Height*4U, outsize);
bool result = ConvertToRGBA8888((const uint8_t*)format.m_DefaultData, header.m_Width, header.m_Height, format.m_PixelFormat, expected_rgba);
ASSERT_TRUE(result);
ASSERT_ARRAY_EQ_LEN(expected_rgba, out, sizeof(out));
dmTexc::Destroy(texture);
}
}
static void ComparePixel(uint8_t* expected, uint8_t* current, uint32_t num_channels)
{
ASSERT_ARRAY_EQ_LEN(expected, current, num_channels);
}
TEST_F(TexcTest, Resize)
{
uint8_t orig[(4*4)*4];
uint8_t resized[(4*4)*4];
// original/resized sizes
uint32_t owidth = 2;
uint32_t oheight = 2;
uint32_t rwidth = 4;
uint32_t rheight = 4;
for (uint32_t i = 0; i < format_count; ++i)
{
Format& format = formats[i];
dmTexc::HTexture texture = (*format.m_CreateFn)(format.m_CompressionType);
dmTexc::Header header;
dmTexc::GetData(texture, orig, sizeof(orig));
ASSERT_TRUE(dmTexc::Resize(texture, 4, 4));
dmTexc::GetHeader(texture, &header);
ASSERT_EQ(4u, header.m_Width);
ASSERT_EQ(4u, header.m_Height);
dmTexc::GetData(texture, resized, sizeof(resized));
uint32_t bpp = 4;
uint32_t ox,oy,rx,ry;
// Check the four corners
ox=0; oy=0; rx=0; ry=0;
ComparePixel(&orig[ox*bpp+oy*owidth*bpp], &resized[rx*bpp+ry*rwidth*bpp], 4);
ox=owidth-1; oy=0; rx=rwidth-1; ry=0;
ComparePixel(&orig[ox*bpp+oy*owidth*bpp], &resized[rx*bpp+ry*rwidth*bpp], 4);
ox=0; oy=oheight-1; rx=0; ry=rheight-1;
ComparePixel(&orig[ox*bpp+oy*owidth*bpp], &resized[rx*bpp+ry*rwidth*bpp], 4);
ox=owidth-1; oy=oheight-1; rx=rwidth-1; ry=rheight-1;
ComparePixel(&orig[ox*bpp+oy*owidth*bpp], &resized[rx*bpp+ry*rwidth*bpp], 4);
dmTexc::Destroy(texture);
}
}
TEST_F(TexcTest, PreMultipliedAlpha)
{
// We convert to 32 bit formats internally, with default alpha
for (uint32_t i = 0; i < format_count; ++i)
{
Format& format = formats[i];
dmTexc::HTexture texture = (*format.m_CreateFn)(format.m_CompressionType);
ASSERT_TRUE(dmTexc::PreMultiplyAlpha(texture));
dmTexc::Destroy(texture);
}
}
TEST_F(TexcTest, MipMaps)
{
for (uint32_t i = 0; i < format_count; ++i)
{
Format& format = formats[i];
dmTexc::HTexture texture = (*format.m_CreateFn)(format.m_CompressionType);
ASSERT_TRUE(dmTexc::GenMipMaps(texture));
dmTexc::Destroy(texture);
}
}
#define ASSERT_RGBA(exp, act)\
ASSERT_EQ((exp)[0], (act)[0]);\
ASSERT_EQ((exp)[1], (act)[1]);\
ASSERT_EQ((exp)[2], (act)[2]);\
ASSERT_EQ((exp)[3], (act)[3]);\
TEST_F(TexcTest, FlipAxis)
{
/* Original image:
* +--------+--------+
* | red | green |
* +--------+--------+
* | blue | white |
* +--------+--------+
*/
const uint8_t red[4] = {255, 0, 0, 255};
const uint8_t green[4] = { 0, 255, 0, 255};
const uint8_t blue[4] = { 0, 0, 255, 255};
const uint8_t white[4] = {255, 255, 255, 255};
uint8_t out[4*4];
dmTexc::HTexture texture = CreateDefaultRGBA32(dmTexc::CT_DEFAULT);
// Original values
dmTexc::GetData(texture, out, 16);
ASSERT_RGBA(out, red);
ASSERT_RGBA(out+4, green);
ASSERT_RGBA(out+8, blue);
ASSERT_RGBA(out+12, white);
/* Flip X axis:
* +--------+--------+
* | green | red |
* +--------+--------+
* | white | blue |
* +--------+--------+
*/
ASSERT_TRUE(dmTexc::Flip(texture, dmTexc::FLIP_AXIS_X));
dmTexc::GetData(texture, out, 16);
ASSERT_RGBA(out, green);
ASSERT_RGBA(out+4, red);
ASSERT_RGBA(out+8, white);
ASSERT_RGBA(out+12, blue);
/* Flip Y axis:
* +--------+--------+
* | white | blue |
* +--------+--------+
* | green | red |
* +--------+--------+
*/
ASSERT_TRUE(dmTexc::Flip(texture, dmTexc::FLIP_AXIS_Y));
dmTexc::GetData(texture, out, 16);
ASSERT_RGBA(out, white);
ASSERT_RGBA(out+4, blue);
ASSERT_RGBA(out+8, green);
ASSERT_RGBA(out+12, red);
// Flip Z axis (no change)
ASSERT_TRUE(dmTexc::Flip(texture, dmTexc::FLIP_AXIS_Z));
dmTexc::GetData(texture, out, 16);
ASSERT_RGBA(out, white);
ASSERT_RGBA(out+4, blue);
ASSERT_RGBA(out+8, green);
ASSERT_RGBA(out+12, red);
dmTexc::Destroy(texture);
}
#undef ASSERT_RGBA
// static void PrintTexture(const char* msg, uint32_t* data, uint32_t width, uint32_t height)
// {
// printf("%s\n", msg);
// for (uint32_t y = 0; y < height; ++y)
// {
// for (uint32_t x = 0; x < width; ++x)
// {
// printf("0x%08X,", data[x + width * y]);
// }
// printf("\n");
// }
// }
static void PrintTexture(const char* msg, uint32_t* data, uint32_t width, uint32_t height)
{
(void)msg, (void)data, (void)width, (void)height;
}
TEST(Helpers, FlipY)
{
const uint32_t width = 8;
const uint32_t height = 8;
uint32_t image[width*height];
for (uint32_t y = 0; y < height; ++y)
{
for (uint32_t x = 0; x < width; ++x)
{
image[x + width * y] = x + width * (height - y - 1);
}
}
PrintTexture("\nBEFORE", image, width, height);
dmTexc::FlipImageY_RGBA8888(image, width, height);
PrintTexture("\nAFTER", image, width, height);
for (uint32_t y = 0; y < height; ++y)
{
for (uint32_t x = 0; x < width; ++x)
{
ASSERT_EQ(x + width * y, image[x + width * y]);
}
}
int w = 7;
int h = 7;
for (uint32_t y = 0; y < h; ++y)
{
for (uint32_t x = 0; x < w; ++x)
{
image[x + w * y] = x + w * (h - y - 1);
}
}
PrintTexture("\nBEFORE", image, w, h);
dmTexc::FlipImageY_RGBA8888(image, w, h);
PrintTexture("\nAFTER", image, w, h);
for (uint32_t y = 0; y < h; ++y)
{
for (uint32_t x = 0; x < w; ++x)
{
ASSERT_EQ(x + w * y, image[x + w * y]);
}
}
}
TEST(Helpers, FlipX)
{
const uint32_t width = 8;
const uint32_t height = 8;
uint32_t image[width*height];
for (uint32_t y = 0; y < height; ++y)
{
for (uint32_t x = 0; x < width; ++x)
{
image[x + width * y] = (width - x - 1) + width * y;
}
}
PrintTexture("\nBEFORE", image, width, height);
dmTexc::FlipImageX_RGBA8888(image, width, height);
PrintTexture("\nAFTER", image, width, height);
for (uint32_t y = 0; y < height; ++y)
{
for (uint32_t x = 0; x < width; ++x)
{
ASSERT_EQ(x + width * y, image[x + width * y]);
}
}
int w = 7;
int h = 7;
for (uint32_t y = 0; y < h; ++y)
{
for (uint32_t x = 0; x < w; ++x)
{
image[x + w * y] = (w - x - 1) + w * y;
}
}
PrintTexture("\nBEFORE", image, w, h);
dmTexc::FlipImageX_RGBA8888(image, w, h);
PrintTexture("\nAFTER", image, w, h);
for (uint32_t y = 0; y < h; ++y)
{
for (uint32_t x = 0; x < w; ++x)
{
ASSERT_EQ(x + w * y, image[x + w * y]);
}
}
}
struct CompileInfo
{
const char* m_Path;
dmTexc::CompressionType m_CompressionType;
dmTexc::PixelFormat m_InputFormat;
dmTexc::PixelFormat m_OutputFormat;
dmTexc::ColorSpace m_ColorSpace;
};
CompileInfo compile_info[] =
{
{"src/test/data/a.png", dmTexc::CT_DEFAULT, dmTexc::PF_R8G8B8A8, dmTexc::PF_R5G6B5, dmTexc::CS_SRGB},
};
class TexcCompileTest : public jc_test_params_class<CompileInfo>
{
protected:
virtual void SetUp()
{
const CompileInfo& info = GetParam();
uint8_t* image = stbi_load(info.m_Path, &m_Width, &m_Height, 0, 0);
ASSERT_TRUE(image != 0);
m_Texture = dmTexc::Create(m_Width, m_Height, info.m_InputFormat, info.m_ColorSpace, info.m_CompressionType, image);
printf("image: %s %u x %u\n", info.m_Path, m_Width, m_Height);
}
virtual void TearDown()
{
dmTexc::Destroy(m_Texture);
}
dmTexc::HTexture m_Texture;
int m_Width;
int m_Height;
};
TEST_P(TexcCompileTest, FlipX)
{
ASSERT_TRUE(dmTexc::Flip(m_Texture, dmTexc::FLIP_AXIS_X));
}
TEST_P(TexcCompileTest, FlipY)
{
ASSERT_TRUE(dmTexc::Flip(m_Texture, dmTexc::FLIP_AXIS_Y));
}
TEST_P(TexcCompileTest, PreMultiplyAlpha)
{
ASSERT_TRUE(dmTexc::PreMultiplyAlpha(m_Texture));
}
TEST_P(TexcCompileTest, GenMipMaps)
{
ASSERT_TRUE(dmTexc::GenMipMaps(m_Texture));
}
TEST_P(TexcCompileTest, Encode)
{
const CompileInfo& info = GetParam();
ASSERT_TRUE(dmTexc::Encode(m_Texture, info.m_OutputFormat, info.m_ColorSpace, dmTexc::CL_BEST, info.m_CompressionType, true, 1));
}
INSTANTIATE_TEST_CASE_P(TexcCompileTest, TexcCompileTest, jc_test_values_in(compile_info));
int main(int argc, char **argv)
{
jc_test_init(&argc, argv);
int ret = jc_test_run_all();
return ret;
}