Repository navigation
Expand file tree
/
Copy pathastc_test_functional.py
More file actions
2155 lines (1755 loc) · 71.6 KB
/
Copy pathastc_test_functional.py
File metadata and controls
2155 lines (1755 loc) · 71.6 KB
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# -----------------------------------------------------------------------------
# Copyright 2020-2021 Arm Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# -----------------------------------------------------------------------------
"""
The functional test runner is a set of tests that validate the ``astcenc``
command line is correctly handled, under both valid and invalid usage
scenarios. These tests do NOT validate the compression codec itself, beyond
some very basic incidental usage needed to validate the command line.
Due to the need to validate pixel colors in test images for both LDR and HDR
images, these tests rely on an HDRI-enabled build of ImageMagic being available
on the system path. To test if the version of ImageMagic on your system is
HDRI-enabled run:
convert --version
... and check that the string "HDRI" is present in the listed features.
Test Tiles
==========
Some basic test images, each 8x8 texels and built up from 4 no. 4x4 texel
constant color blocks, are used to help determine that the command line is
being processed correctly.
LDR Test Pattern
----------------
LDR images are an 8x8 image containing 4 4x4 constant color blocks. Assuming
(0, 0) is the top left (TL), the component uncompressed block colors are:
* (0, 0) TL = Black, opaque = (0.00, 0.00, 0.00, 1.00)
* (7, 0) TR = Red, opaque = (1.00, 0.00, 0.00, 1.00)
* (0, 7) BL = White, opaque = (1.00, 1.00, 1.00, 1.00)
* (7, 7) BR = Green, trans = (0.25, 0.75, 0.00, 0.87)
HDR Test Pattern
----------------
HDR images are an 8x8 image containing 4 4x4 constant color blocks. Assuming
(0, 0) is the top left (TL), the component uncompressed block colors are:
* (0, 0) TL = LDR Black, opaque = (0.00, 0.00, 0.00, 1.00)
* (7, 0) TR = HDR Red, opaque = (8.00, 0.00, 0.00, 1.00)
* (0, 7) BL = HDR White, opaque = (3.98, 3.98, 3.98, 1.00)
* (7, 7) BR = LDR Green, trans = (0.25, 0.75, 0.00, 0.87)
"""
import argparse
import filecmp
import os
import re
import signal
import string
import subprocess as sp
import sys
import tempfile
import time
import unittest
import numpy
from PIL import Image
import testlib.encoder as te
import testlib.image as tli
# Enable these to always write out, irrespective of test result
ASTCENC_CLI_ALWAYS = False
ASTCENC_LOG_ALWAYS = False
# Enable these to write out on failure for positive tests
ASTCENC_CLI_ON_ERROR = True
ASTCENC_LOG_ON_ERROR = True
# Enable these to write out on failure for negative tests
ASTCENC_CLI_ON_ERROR_NEG = True
ASTCENC_LOG_ON_ERROR_NEG = True
# LDR test pattern
ASTCENC_TEST_PATTERN_LDR = {
"TL": (0.00, 0.00, 0.00, 1.00),
"TR": (1.00, 0.00, 0.00, 1.00),
"BL": (1.00, 1.00, 1.00, 1.00),
"BR": (0.25, 0.75, 0.00, 0.87)
}
# HDR test pattern
ASTCENC_TEST_PATTERN_HDR = {
"TL": (0.00, 0.00, 0.00, 1.00),
"TR": (8.00, 0.00, 0.00, 1.00),
"BL": (3.98, 3.98, 3.98, 1.00),
"BR": (0.25, 0.75, 0.00, 0.87)
}
LDR_RGB_PSNR_PATTERN = re.compile(r"\s*PSNR \(LDR-RGB\): (.*) dB")
g_TestEncoder = "avx2"
class CLITestBase(unittest.TestCase):
"""
Command line interface base class.
These tests are designed to test the command line is handled correctly.
They are not detailed tests of the codec itself; only basic sanity checks
that some type of processing occurred are used.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
encoder = te.Encoder2x(g_TestEncoder)
self.binary = encoder.binary
def setUp(self):
"""
Set up a test case.
Create a new temporary directory for output files.
"""
self.tempDir = tempfile.TemporaryDirectory()
def tearDown(self):
"""
Tear down a test case.
Clean up the temporary directory created for output files.
"""
self.tempDir.cleanup()
self.tempDir = None
@staticmethod
def get_ref_image_path(profile, mode, image):
"""
Get the path of a reference image on disk.
Args:
profile (str): The color profile.
mode (str): The type of image to load.
image (str): The image variant to load.
Returns:
str: The path to the test image file on disk.
"""
nameMux = {
"LDR": {
"input": "png",
"comp": "astc"
},
"LDRS": {
"input": "png",
"comp": "astc"
},
"HDR": {
"input": "exr",
"comp": "astc"
}
}
assert profile in nameMux.keys()
assert mode in nameMux["LDR"].keys()
scriptDir = os.path.dirname(__file__)
fileName = "%s-%s-1x1.%s" % (profile, image, nameMux[profile][mode])
return os.path.join(scriptDir, "Data", fileName)
def get_tmp_image_path(self, profile, mode):
"""
Get the path of a temporary output image on disk.
Temporary files are automatically cleaned up when the test tearDown
occurs.
Args:
profile (str): The color profile. "EXP" means explicit which means
the "mode" parameter is interpreted as a literal file
extension not a symbolic mode.
mode (str): The type of image to load.
Returns:
str: The path to the test image file on disk.
"""
# Handle explicit mode
if profile == "EXP":
tmpFile, tmpPath = tempfile.mkstemp(mode, dir=self.tempDir.name)
os.close(tmpFile)
os.remove(tmpPath)
return tmpPath
# Handle symbolic modes
nameMux = {
"LDR": {
"comp": ".astc",
"decomp": ".png",
"bad": ".foo"
},
"LDRS": {
"comp": ".astc",
"decomp": ".png",
"bad": ".foo"
},
"HDR": {
"comp": ".astc",
"decomp": ".exr",
"bad": ".foo"
}
}
assert profile in nameMux.keys()
assert mode in nameMux["LDR"].keys()
suffix = nameMux[profile][mode]
tmpFile, tmpPath = tempfile.mkstemp(suffix, dir=self.tempDir.name)
os.close(tmpFile)
os.remove(tmpPath)
return tmpPath
class CLIPTest(CLITestBase):
"""
Command line interface positive tests.
These tests are designed to test the command line is handled correctly.
They are not detailed tests of the codec itself; only basic sanity checks
that some type of processing occurred are used.
"""
def compare(self, image1, image2):
"""
Utility function to compare two images.
Note that this comparison tests only decoded color values; any file
metadata is ignored, and encoding methods are not compared.
Args:
image1 (str): Path to the first image.
image2 (str): Path to the second image.
Returns:
bool: ``True` if the images are the same, ``False`` otherwise.
"""
img1 = Image.open(image1)
img2 = Image.open(image2)
# Images must have same size
if img1.size != img2.size:
print("Size")
return False
# Images must have same number of color channels
if img1.getbands() != img2.getbands():
# ... unless the only different is alpha
self.assertEqual(img1.getbands(), ("R", "G", "B"))
self.assertEqual(img2.getbands(), ("R", "G", "B", "A"))
# ... and the alpha is always one
bands = img2.split()
alphaHist = bands[3].histogram()
self.assertEqual(sum(alphaHist[:-1]), 0)
# Generate a version of img2 without alpha
img2 = Image.merge("RGB", (bands[0], bands[1], bands[2]))
# Compute sum of absolute differences
dat1 = numpy.array(img1)
dat2 = numpy.array(img2)
sad = numpy.sum(numpy.abs(dat1 - dat2))
if sad != 0:
print(img1.load()[0, 0])
print(img2.load()[0, 0])
return sad == 0
def get_channel_rmse(self, image1, image2):
"""
Get the channel-by-channel root mean square error.
Args:
image1 (str): Path to the first image.
image2 (str): Path to the second image.
Returns:
tuple: Tuple of floats containing the RMSE per channel.
None: Images could not be compared because they are different size.
"""
img1 = Image.open(image1)
img2 = Image.open(image2)
# Images must have same size
if img1.size != img2.size:
return None
# Images must have same number of color channels
if img1.getbands() != img2.getbands():
# ... unless the only different is alpha
self.assertEqual(img1.getbands(), ("R", "G", "B"))
self.assertEqual(img2.getbands(), ("R", "G", "B", "A"))
# ... and the alpha is always one
bands = img2.split()
alphaHist = bands[3].histogram()
self.assertEqual(sum(alphaHist[:-1]), 0)
# Generate a version of img2 without alpha
img2 = Image.merge("RGB", (bands[0], bands[1], bands[2]))
# Compute root mean square error
img1bands = img1.split()
img2bands = img2.split()
rmseVals = []
imgBands = zip(img1bands, img2bands)
for img1Ch, img2Ch in imgBands:
imSz = numpy.prod(img1Ch.size)
dat1 = numpy.array(img1Ch)
dat2 = numpy.array(img2Ch)
sad = numpy.sum(numpy.square(dat1 - dat2))
mse = numpy.divide(sad, imSz)
rmse = numpy.sqrt(mse)
rmseVals.append(rmse)
return rmseVals
@staticmethod
def get_color_refs(mode, corners):
"""
Build a set of reference colors from apriori color list.
Args:
mode (str): The color mode (LDR, or HDR)
corners (str or list): The corner or list of corners -- named TL,
TR, BL, and BR -- to return.
Returns:
tuple: The color value, if corners was a name.
[tuple]: List of color values, if corners was a list of names.
"""
modes = {
"LDR": ASTCENC_TEST_PATTERN_LDR,
"HDR": ASTCENC_TEST_PATTERN_HDR
}
if isinstance(corners, str):
return [modes[mode][corners]]
return [modes[mode][corner] for corner in corners]
def assertColorSame(self, colorRef, colorNew, threshold=0.02, swiz=None):
"""
Test if a color is the similar to a reference.
Will trigger a test failure if the colors are not within threshold.
Args:
colorRef (tuple): The reference color to compare with.
colorNew (tuple): The new color.
threshold (float): The allowed deviation from colorRef (ratio).
swiz (str): The swizzle string (4 characters from the set
`rgba01`), applied to the reference color.
"""
self.assertEqual(len(colorRef), len(colorNew))
# Swizzle the reference color if needed
if swiz:
self.assertEqual(len(swiz), len(colorRef))
remap = {
"0": len(colorRef),
"1": len(colorRef) + 1
}
if len(colorRef) >= 1:
remap["r"] = 0
if len(colorRef) >= 2:
remap["g"] = 1
if len(colorRef) >= 3:
remap["b"] = 2
if len(colorRef) >= 4:
remap["a"] = 3
colorRefExt = list(colorRef) + [0.0, 1.0]
colorRef = [colorRefExt[remap[s]] for s in swiz]
for chRef, chNew in zip(colorRef, colorNew):
deltaMax = chRef * threshold
self.assertAlmostEqual(chRef, chNew, delta=deltaMax)
def exec(self, command, pattern=None):
"""
Execute a positive test.
Will trigger a test failure if the subprocess return code is any value
other than zero.
Args:
command (list(str)): The command to execute.
pattern (re.Pattern): The regex pattern to search for, must
contain a single group (this is returned to the caller). The
test will fail if no pattern match is found.
Returns:
str: The stdout output of the child process, or the first group
from the passed regex pattern.
"""
try:
result = sp.run(command, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=True, check=True)
error = False
except sp.CalledProcessError as ex:
result = ex
error = True
# Emit debug logging if needed
if ASTCENC_CLI_ALWAYS or (error and ASTCENC_CLI_ON_ERROR):
# Format for shell replay
print("\n" + " ".join(command))
# Format for script command list replay
print("\n" + ", ".join(("\"%s\"" % x for x in command)))
if ASTCENC_LOG_ALWAYS or (error and ASTCENC_LOG_ON_ERROR):
print(result.stdout)
rcode = result.returncode
if rcode < 0:
msg = "Exec died with signal %s" % signal.Signals(-rcode).name
self.assertGreaterEqual(rcode, 0, msg)
if rcode > 0:
msg = "Exec died with application error %u" % rcode
self.assertEqual(rcode, 0, msg)
# If there is a regex pattern provided, then search for it
if pattern:
match = pattern.search(result.stdout)
self.assertIsNotNone(match)
return match.group(1)
return result.stdout
def test_ldr_compress(self):
"""
Test basic LDR compression.
"""
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("LDR", "comp")
imRef = self.get_ref_image_path("LDR", "comp", "A")
command = [self.binary, "-cl", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(filecmp.cmp(imRef, imOut, False))
def test_srgb_compress(self):
"""
Test basic LDR sRGB compression.
"""
imIn = self.get_ref_image_path("LDRS", "input", "A")
imOut = self.get_tmp_image_path("LDRS", "comp")
imRef = self.get_ref_image_path("LDRS", "comp", "A")
command = [self.binary, "-cs", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(filecmp.cmp(imRef, imOut, False))
def test_hdr_compress1(self):
"""
Test basic HDR + LDR alpha compression.
"""
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("HDR", "comp")
imRef = self.get_ref_image_path("HDR", "comp", "A")
command = [self.binary, "-ch", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(filecmp.cmp(imRef, imOut, False))
def test_hdr_compress2(self):
"""
Test basic HDR + HDR alpha compression.
"""
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("HDR", "comp")
imRef = self.get_ref_image_path("HDR", "comp", "A")
command = [self.binary, "-cH", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(filecmp.cmp(imRef, imOut, False))
def test_ldr_decompress(self):
"""
Test basic LDR decompression.
"""
imIn = self.get_ref_image_path("LDR", "comp", "A")
imOut = self.get_tmp_image_path("LDR", "decomp")
imRef = self.get_ref_image_path("LDR", "input", "A")
command = [self.binary, "-dl", imIn, imOut]
self.exec(command)
self.assertTrue(self.compare(imRef, imOut))
def test_srgb_decompress(self):
"""
Test basic LDR sRGB decompression.
"""
imIn = self.get_ref_image_path("LDRS", "comp", "A")
imOut = self.get_tmp_image_path("LDRS", "decomp")
imRef = self.get_ref_image_path("LDRS", "input", "A")
command = [self.binary, "-ds", imIn, imOut]
self.exec(command)
self.assertTrue(self.compare(imRef, imOut))
def test_hdr_decompress1(self):
"""
Test basic HDR + LDR alpha decompression.
"""
imIn = self.get_ref_image_path("HDR", "comp", "A")
imOut = self.get_tmp_image_path("HDR", "decomp")
imRef = self.get_ref_image_path("HDR", "input", "A")
command = [self.binary, "-dh", imIn, imOut]
self.exec(command)
colRef = tli.Image(imRef).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colRef, colOut)
def test_hdr_decompress2(self):
"""
Test basic HDR + HDR alpha decompression.
"""
imIn = self.get_ref_image_path("HDR", "comp", "A")
imOut = self.get_tmp_image_path("HDR", "decomp")
imRef = self.get_ref_image_path("HDR", "input", "A")
command = [self.binary, "-dH", imIn, imOut]
self.exec(command)
colRef = tli.Image(imRef).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colRef, colOut)
def test_ldr_roundtrip(self):
"""
Test basic LDR round-trip
"""
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("LDR", "decomp")
command = [self.binary, "-tl", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(self.compare(imIn, imOut))
def test_srgb_roundtrip(self):
"""
Test basic LDR sRGB round-trip
"""
imIn = self.get_ref_image_path("LDRS", "input", "A")
imOut = self.get_tmp_image_path("LDRS", "decomp")
command = [self.binary, "-ts", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
self.assertTrue(self.compare(imIn, imOut))
def test_hdr_roundtrip1(self):
"""
Test basic HDR + LDR alpha round-trip.
"""
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("HDR", "decomp")
command = [self.binary, "-th", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
colIn = tli.Image(imIn).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colIn, colOut)
def test_hdr_roundtrip2(self):
"""
Test basic HDR + HDR alpha round-trip.
"""
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("HDR", "decomp")
command = [self.binary, "-tH", imIn, imOut, "6x6", "-exhaustive"]
self.exec(command)
colIn = tli.Image(imIn).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colIn, colOut)
def test_valid_2d_block_sizes(self):
"""
Test all valid block sizes are accepted (2D images).
"""
blockSizes = [
"4x4", "5x4", "5x5", "6x5", "6x6", "8x5", "8x6",
"10x5", "10x6", "8x8", "10x8", "10x10", "12x10", "12x12"
]
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("LDR", "decomp")
for blk in blockSizes:
with self.subTest(blockSize=blk):
command = [self.binary, "-tl", imIn, imOut, blk, "-exhaustive"]
self.exec(command)
colIn = tli.Image(imIn).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colIn, colOut)
def test_valid_3d_block_sizes(self):
"""
Test all valid block sizes are accepted (3D images).
"""
blockSizes = [
"3x3x3",
"4x3x3", "4x4x3", "4x4x4",
"5x4x4", "5x5x4", "5x5x5",
"6x5x5", "6x6x5", "6x6x6"
]
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("LDR", "decomp")
for blk in blockSizes:
with self.subTest(blockSize=blk):
command = [self.binary, "-tl", imIn, imOut, blk, "-exhaustive"]
self.exec(command)
colIn = tli.Image(imIn).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colIn, colOut)
def test_valid_presets(self):
"""
Test all valid presets are accepted
"""
presets = ["-fastest", "-fast", "-medium", "-thorough", "-exhaustive"]
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("LDR", "decomp")
for preset in presets:
with self.subTest(preset=preset):
command = [self.binary, "-tl", imIn, imOut, "4x4", preset]
self.exec(command)
colIn = tli.Image(imIn).get_colors((0, 0))
colOut = tli.Image(imOut).get_colors((0, 0))
self.assertColorSame(colIn, colOut)
def test_valid_ldr_input_formats(self):
"""
Test valid LDR input file formats.
"""
imgFormats = ["bmp", "dds", "jpg", "ktx", "png", "tga"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = "./Test/Data/Tiles/ldr.%s" % imgFormat
imOut = self.get_tmp_image_path("LDR", "decomp")
command = [self.binary, "-tl", imIn, imOut, "4x4", "-fast"]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut).get_colors((7, 7))
self.assertColorSame(colIn, colOut)
def test_valid_uncomp_ldr_output_formats(self):
"""
Test valid uncompressed LDR output file formats.
"""
imgFormats = ["bmp", "dds", "ktx", "png", "tga"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("EXP", ".%s" % imgFormat)
command = [self.binary, "-tl", imIn, imOut, "4x4", "-fast"]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut).get_colors((7, 7))
self.assertColorSame(colIn, colOut)
def test_valid_comp_ldr_output_formats(self):
"""
Test valid compressed LDR output file formats.
"""
imgFormats = ["astc", "ktx"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = self.get_ref_image_path("LDR", "input", "A")
imOut = self.get_tmp_image_path("EXP", ".%s" % imgFormat)
imOut2 = self.get_tmp_image_path("LDR", "decomp")
command = [self.binary, "-cl", imIn, imOut, "4x4", "-fast"]
self.exec(command)
command = [self.binary, "-dl", imOut, imOut2]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut2).get_colors((7, 7))
self.assertColorSame(colIn, colOut2)
def test_valid_hdr_input_formats(self):
"""
Test valid HDR input file formats.
"""
imgFormats = ["exr", "hdr"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = "./Test/Data/Tiles/hdr.%s" % imgFormat
imOut = self.get_tmp_image_path("HDR", "decomp")
command = [self.binary, "-th", imIn, imOut, "4x4", "-fast"]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat, profile="hdr"):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut).get_colors((7, 7))
self.assertColorSame(colIn, colOut)
def test_valid_uncomp_hdr_output_formats(self):
"""
Test valid uncompressed HDR output file formats.
"""
imgFormats = ["dds", "exr", "hdr", "ktx"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("EXP", ".%s" % imgFormat)
command = [self.binary, "-th", imIn, imOut, "4x4", "-fast"]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat, profile="hdr"):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut).get_colors((7, 7))
self.assertColorSame(colIn, colOut)
def test_valid_comp_hdr_output_formats(self):
"""
Test valid compressed HDR output file formats.
"""
imgFormats = ["astc", "ktx"]
for imgFormat in imgFormats:
with self.subTest(imgFormat=imgFormat):
imIn = self.get_ref_image_path("HDR", "input", "A")
imOut = self.get_tmp_image_path("EXP", ".%s" % imgFormat)
imOut2 = self.get_tmp_image_path("HDR", "decomp")
command = [self.binary, "-ch", imIn, imOut, "4x4", "-fast"]
self.exec(command)
command = [self.binary, "-dh", imOut, imOut2]
self.exec(command)
# Check colors if image wrapper supports it
if tli.Image.is_format_supported(imgFormat):
colIn = tli.Image(imIn).get_colors((7, 7))
colOut = tli.Image(imOut2).get_colors((7, 7))
self.assertColorSame(colIn, colOut2)
def test_compress_mask(self):
"""
Test compression of mask textures.
"""
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Images/Small/LDR-RGB/ldr-rgb-10.png",
decompFile, "4x4", "-exhaustive"]
noMaskdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
command.append("-mask")
maskdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
# Note that this test simply asserts that the "-mask" is connected and
# affects the output. We don't test it does something useful; that it
# outside the scope of this test case.
self.assertNotEqual(noMaskdB, maskdB)
def test_compress_normal_psnr(self):
"""
Test compression of normal textures using PSNR error metrics.
"""
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Images/Small/LDR-XY/ldr-xy-00.png",
decompFile, "5x5", "-exhaustive"]
refdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
command.append("-normal")
testdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
# Note that this test simply asserts that the "-normal_psnr" is
# connected and affects the output. We don't test it does something
# useful; that it outside the scope of this test case.
self.assertNotEqual(refdB, testdB)
def test_compress_normal_percep(self):
"""
Test compression of normal textures using perceptual error metrics.
"""
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Images/Small/LDR-XY/ldr-xy-00.png",
decompFile, "4x4", "-exhaustive"]
refdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
command.append("-normal")
command.append("-perceptual")
testdB = float(self.exec(command, LDR_RGB_PSNR_PATTERN))
# Note that this test simply asserts that the "-normal -percep" is
# connected and affects the output. We don't test it does something
# useful; that it outside the scope of this test case.
self.assertNotEqual(refdB, testdB)
def test_compress_esw(self):
"""
Test compression swizzles.
"""
# The swizzles to test
swizzles = ["rgba", "g0r1", "rrrg"]
# Compress a swizzled image
for swizzle in swizzles:
with self.subTest(swizzle=swizzle):
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Data/Tiles/ldr.png",
decompFile, "4x4", "-exhaustive",
"-esw", swizzle]
self.exec(command)
# Fetch the three color
img = tli.Image(decompFile)
colorVal = img.get_colors([(7, 7)])
colorRef = self.get_color_refs("LDR", "BR")
self.assertColorSame(colorRef[0], colorVal[0], swiz=swizzle)
def test_compress_dsw(self):
"""
Test decompression swizzles.
"""
# The swizzles to test
swizzles = ["rgba", "g0r1", "rrrg"]
# Decompress a swizzled image
for swizzle in swizzles:
with self.subTest(swizzle=swizzle):
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Data/Tiles/ldr.png",
decompFile, "4x4", "-exhaustive",
"-dsw", swizzle]
self.exec(command)
# Fetch the three color
img = tli.Image(decompFile)
colorVal = img.get_colors([(7, 7)])
colorRef = self.get_color_refs("LDR", "BR")
self.assertColorSame(colorRef[0], colorVal[0], swiz=swizzle)
def test_compress_esw_dsw(self):
"""
Test compression and decompression swizzles
"""
# Compress a swizzled image, and swizzle back in decompression
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",
"./Test/Data/Tiles/ldr.png",
decompFile, "4x4", "-exhaustive",
"-esw", "gbar", "-dsw", "argb"]
self.exec(command)
# Fetch the three color
img = tli.Image(decompFile)
colorVal = img.get_colors([(7, 7)])
colorRef = self.get_color_refs("LDR", "BR")
self.assertColorSame(colorRef[0], colorVal[0])
def test_compress_flip(self):
"""
Test LDR image flip on compression.
"""
# Compress a flipped image
compFile = self.get_tmp_image_path("LDR", "comp")
command = [
self.binary, "-cl",
"./Test/Data/Tiles/ldr.png",
compFile, "4x4", "-fast", "-yflip"]
self.exec(command)
# Decompress a non-flipped image
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-dl",
compFile,
decompFile]
self.exec(command)
# Compare TL (0, 0) with BL - should match
colorRef = self.get_color_refs("LDR", "BL")
img = tli.Image(decompFile)
colorVal = img.get_colors([(0, 0)])
self.assertColorSame(colorRef[0], colorVal[0])
def test_decompress_flip(self):
"""
Test LDR image flip on decompression.
"""
# Compress a non-flipped image
compFile = self.get_tmp_image_path("LDR", "comp")
command = [
self.binary, "-cl",
"./Test/Data/Tiles/ldr.png",
compFile, "4x4", "-fast"]
self.exec(command)
# Decompress a flipped image
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-dl",
compFile,
decompFile, "-yflip"]
self.exec(command)
# Compare TL (0, 0) with BL - should match
colorRef = self.get_color_refs("LDR", "BL")
img = tli.Image(decompFile)
colorVal = img.get_colors([(0, 0)])
self.assertColorSame(colorRef[0], colorVal[0])
def test_roundtrip_flip(self):
"""
Test LDR image flip on roundtrip (no flip should occur).
"""
# Compress and decompressed a flipped LDR image
decompFile = self.get_tmp_image_path("LDR", "decomp")
command = [
self.binary, "-tl",