| 
10 | 10 | import random  | 
11 | 11 | import re  | 
12 | 12 | import shutil  | 
13 |  | -import sys  | 
14 | 13 | import time  | 
15 | 14 | import unittest  | 
16 | 15 | from pathlib import Path  | 
@@ -6807,117 +6806,97 @@ def test_poppler(self):  | 
6807 | 6806 |   @no_wasm64('MEMORY64 does not yet support SJLJ')  | 
6808 | 6807 |   @is_slow_test  | 
6809 | 6808 |   def test_openjpeg(self):  | 
6810 |  | -    def do_test_openjpeg():  | 
6811 |  | -      def line_splitter(data):  | 
6812 |  | -        out = ''  | 
6813 |  | -        counter = 0  | 
6814 |  | - | 
6815 |  | -        for ch in data:  | 
6816 |  | -          out += ch  | 
6817 |  | -          if ch == ' ' and counter > 60:  | 
6818 |  | -            out += '\n'  | 
6819 |  | -            counter = 0  | 
6820 |  | -          else:  | 
6821 |  | -            counter += 1  | 
6822 |  | - | 
6823 |  | -        return out  | 
6824 |  | - | 
6825 |  | -      # remove -g, so we have one test without it by default  | 
6826 |  | -      self.emcc_args = [x for x in self.emcc_args if x != '-g']  | 
6827 |  | - | 
6828 |  | -      original_j2k = test_file('openjpeg/syntensity_lobby_s.j2k')  | 
6829 |  | -      image_bytes = list(bytearray(read_binary(original_j2k)))  | 
6830 |  | -      create_file('pre.js', """  | 
6831 |  | -        Module.preRun = () => FS.createDataFile('/', 'image.j2k', %s, true, false, false);  | 
6832 |  | -        Module.postRun = () => {  | 
6833 |  | -          out('Data: ' + JSON.stringify(Array.from(FS.readFile('image.raw'))));  | 
6834 |  | -        };  | 
6835 |  | -        """ % line_splitter(str(image_bytes)))  | 
6836 |  | - | 
6837 |  | -      # ensure libpng is built so that openjpeg's configure step can detect it.  | 
6838 |  | -      # If we don't do this then we don't know what the state of the cache will be  | 
6839 |  | -      # and this test would different non-deterministic results based on, for example,  | 
6840 |  | -      # what other tests had previously run.  | 
6841 |  | -      builder_cmd = [EMBUILDER, 'build', 'libpng']  | 
6842 |  | -      if self.get_setting('MEMORY64'):  | 
6843 |  | -        builder_cmd.append('--wasm64')  | 
6844 |  | -        self.emcc_args.append('-Wno-pointer-to-int-cast')  | 
6845 |  | -      self.run_process(builder_cmd)  | 
6846 |  | -      lib = self.get_library('third_party/openjpeg',  | 
6847 |  | -                             [Path('codec/CMakeFiles/j2k_to_image.dir/index.c.o'),  | 
6848 |  | -                              Path('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'),  | 
6849 |  | -                              Path('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'),  | 
6850 |  | -                              Path('bin/libopenjpeg.a')],  | 
6851 |  | -                             configure=['cmake', '.'],  | 
6852 |  | -                             # configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'],  | 
6853 |  | -                             make_args=[]) # no -j 2, since parallel builds can fail  | 
6854 |  | - | 
6855 |  | -      # We use doubles in JS, so we get slightly different values than native code. So we  | 
6856 |  | -      # check our output by comparing the average pixel difference  | 
6857 |  | -      def image_compare(output):  | 
6858 |  | -        # Get the image generated by JS, from the JSON.stringify'd array  | 
6859 |  | -        m = re.search(r'\[[\d, -]*\]', output)  | 
6860 |  | -        self.assertIsNotNone(m, 'Failed to find proper image output in: ' + output)  | 
6861 |  | -        # Evaluate the output as a python array  | 
6862 |  | -        js_data = eval(m.group(0))  | 
6863 |  | - | 
6864 |  | -        js_data = [x if x >= 0 else 256 + x for x in js_data] # Our output may be signed, so unsign it  | 
6865 |  | - | 
6866 |  | -        # Get the correct output  | 
6867 |  | -        true_data = bytearray(read_binary(test_file('openjpeg/syntensity_lobby_s.raw')))  | 
6868 |  | - | 
6869 |  | -        # Compare them  | 
6870 |  | -        self.assertEqual(len(js_data), len(true_data))  | 
6871 |  | -        num = len(js_data)  | 
6872 |  | -        diff_total = js_total = true_total = 0  | 
6873 |  | -        for i in range(num):  | 
6874 |  | -          js_total += js_data[i]  | 
6875 |  | -          true_total += true_data[i]  | 
6876 |  | -          diff_total += abs(js_data[i] - true_data[i])  | 
6877 |  | -        js_mean = js_total / float(num)  | 
6878 |  | -        true_mean = true_total / float(num)  | 
6879 |  | -        diff_mean = diff_total / float(num)  | 
6880 |  | - | 
6881 |  | -        image_mean = 83.265  | 
6882 |  | -        # print '[image stats:', js_mean, image_mean, true_mean, diff_mean, num, ']'  | 
6883 |  | -        assert abs(js_mean - image_mean) < 0.01, [js_mean, image_mean]  | 
6884 |  | -        assert abs(true_mean - image_mean) < 0.01, [true_mean, image_mean]  | 
6885 |  | -        assert diff_mean < 0.01, diff_mean  | 
6886 |  | - | 
6887 |  | -        return output  | 
6888 |  | - | 
6889 |  | -      self.emcc_args += ['--minify=0'] # to compare the versions  | 
6890 |  | -      self.emcc_args += ['--pre-js', 'pre.js']  | 
6891 |  | - | 
6892 |  | -      def do_test():  | 
6893 |  | -        self.do_runf('third_party/openjpeg/codec/j2k_to_image.c',  | 
6894 |  | -                     'Successfully generated', # The real test for valid output is in image_compare  | 
6895 |  | -                     args='-i image.j2k -o image.raw'.split(),  | 
6896 |  | -                     emcc_args=['-sUSE_LIBPNG'],  | 
6897 |  | -                     libraries=lib,  | 
6898 |  | -                     includes=[test_file('third_party/openjpeg/libopenjpeg'),  | 
6899 |  | -                               test_file('third_party/openjpeg/codec'),  | 
6900 |  | -                               test_file('third_party/openjpeg/common'),  | 
6901 |  | -                               Path(self.get_build_dir(), 'third_party/openjpeg')],  | 
6902 |  | -                     output_nicerizer=image_compare)  | 
6903 |  | - | 
6904 |  | -      do_test()  | 
6905 |  | - | 
6906 |  | -      # extra testing  | 
6907 |  | -      if self.get_setting('ALLOW_MEMORY_GROWTH') == 1:  | 
6908 |  | -        print('no memory growth', file=sys.stderr)  | 
6909 |  | -        self.set_setting('ALLOW_MEMORY_GROWTH', 0)  | 
6910 |  | -        do_test()  | 
 | 6809 | +    def line_splitter(data):  | 
 | 6810 | +      out = ''  | 
 | 6811 | +      counter = 0  | 
 | 6812 | + | 
 | 6813 | +      for ch in data:  | 
 | 6814 | +        out += ch  | 
 | 6815 | +        if ch == ' ' and counter > 60:  | 
 | 6816 | +          out += '\n'  | 
 | 6817 | +          counter = 0  | 
 | 6818 | +        else:  | 
 | 6819 | +          counter += 1  | 
6911 | 6820 | 
 
  | 
6912 |  | -    if is_sanitizing(self.emcc_args):  | 
6913 |  | -      # In ASan mode we need a large initial memory (or else wasm-ld fails).  | 
6914 |  | -      # The OpenJPEG CMake will build several executables (which we need parts  | 
6915 |  | -      # of in our testing, see above), so we must enable the flag for them all.  | 
6916 |  | -      with env_modify({'EMCC_CFLAGS': '-sINITIAL_MEMORY=300MB'}):  | 
6917 |  | -        self.emcc_args.append('-Wno-unused-command-line-argument')  | 
6918 |  | -        do_test_openjpeg()  | 
6919 |  | -    else:  | 
6920 |  | -      do_test_openjpeg()  | 
 | 6821 | +      return out  | 
 | 6822 | + | 
 | 6823 | +    # remove -g, so we have one test without it by default  | 
 | 6824 | +    self.emcc_args = [x for x in self.emcc_args if x != '-g']  | 
 | 6825 | + | 
 | 6826 | +    original_j2k = test_file('openjpeg/syntensity_lobby_s.j2k')  | 
 | 6827 | +    image_bytes = list(bytearray(read_binary(original_j2k)))  | 
 | 6828 | +    create_file('pre.js', """  | 
 | 6829 | +      Module.preRun = () => FS.createDataFile('/', 'image.j2k', %s, true, false, false);  | 
 | 6830 | +      Module.postRun = () => {  | 
 | 6831 | +        out('Data: ' + JSON.stringify(Array.from(FS.readFile('image.raw'))));  | 
 | 6832 | +      };  | 
 | 6833 | +      """ % line_splitter(str(image_bytes)))  | 
 | 6834 | + | 
 | 6835 | +    # ensure libpng is built so that openjpeg's configure step can detect it.  | 
 | 6836 | +    # If we don't do this then we don't know what the state of the cache will be  | 
 | 6837 | +    # and this test would different non-deterministic results based on, for example,  | 
 | 6838 | +    # what other tests had previously run.  | 
 | 6839 | +    builder_cmd = [EMBUILDER, 'build', 'libpng']  | 
 | 6840 | +    if self.get_setting('MEMORY64'):  | 
 | 6841 | +      builder_cmd.append('--wasm64')  | 
 | 6842 | +      self.emcc_args.append('-Wno-pointer-to-int-cast')  | 
 | 6843 | +    self.run_process(builder_cmd)  | 
 | 6844 | +    lib = self.get_library('third_party/openjpeg',  | 
 | 6845 | +                           [Path('codec/CMakeFiles/j2k_to_image.dir/index.c.o'),  | 
 | 6846 | +                            Path('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'),  | 
 | 6847 | +                            Path('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'),  | 
 | 6848 | +                            Path('bin/libopenjpeg.a')],  | 
 | 6849 | +                           configure=['cmake', '.'],  | 
 | 6850 | +                           # configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'],  | 
 | 6851 | +                           make_args=[]) # no -j 2, since parallel builds can fail  | 
 | 6852 | + | 
 | 6853 | +    # We use doubles in JS, so we get slightly different values than native code. So we  | 
 | 6854 | +    # check our output by comparing the average pixel difference  | 
 | 6855 | +    def image_compare(output):  | 
 | 6856 | +      # Get the image generated by JS, from the JSON.stringify'd array  | 
 | 6857 | +      m = re.search(r'\[[\d, -]*\]', output)  | 
 | 6858 | + | 
 | 6859 | +      self.assertIsNotNone(m, 'Failed to find proper image output in: ' + output)  | 
 | 6860 | +      # Evaluate the output as a python array  | 
 | 6861 | +      js_data = eval(m.group(0))  | 
 | 6862 | + | 
 | 6863 | +      js_data = [x if x >= 0 else 256 + x for x in js_data] # Our output may be signed, so unsign it  | 
 | 6864 | +      # Get the correct output  | 
 | 6865 | +      true_data = bytearray(read_binary(test_file('openjpeg/syntensity_lobby_s.raw')))  | 
 | 6866 | + | 
 | 6867 | +      # Compare them  | 
 | 6868 | +      self.assertEqual(len(js_data), len(true_data))  | 
 | 6869 | +      num = len(js_data)  | 
 | 6870 | +      diff_total = js_total = true_total = 0  | 
 | 6871 | +      for i in range(num):  | 
 | 6872 | +        js_total += js_data[i]  | 
 | 6873 | +        true_total += true_data[i]  | 
 | 6874 | +        diff_total += abs(js_data[i] - true_data[i])  | 
 | 6875 | +      js_mean = js_total / float(num)  | 
 | 6876 | +      true_mean = true_total / float(num)  | 
 | 6877 | +      diff_mean = diff_total / float(num)  | 
 | 6878 | + | 
 | 6879 | +      image_mean = 83.265  | 
 | 6880 | +      # print '[image stats:', js_mean, image_mean, true_mean, diff_mean, num, ']'  | 
 | 6881 | +      assert abs(js_mean - image_mean) < 0.01, [js_mean, image_mean]  | 
 | 6882 | +      assert abs(true_mean - image_mean) < 0.01, [true_mean, image_mean]  | 
 | 6883 | +      assert diff_mean < 0.01, diff_mean  | 
 | 6884 | + | 
 | 6885 | +      return output  | 
 | 6886 | + | 
 | 6887 | +    self.emcc_args += ['--minify=0'] # to compare the versions  | 
 | 6888 | +    self.emcc_args += ['--pre-js', 'pre.js']  | 
 | 6889 | + | 
 | 6890 | +    self.do_runf('third_party/openjpeg/codec/j2k_to_image.c',  | 
 | 6891 | +                 'Successfully generated', # The real test for valid output is in image_compare  | 
 | 6892 | +                 args='-i image.j2k -o image.raw'.split(),  | 
 | 6893 | +                 emcc_args=['-sUSE_LIBPNG'],  | 
 | 6894 | +                 libraries=lib,  | 
 | 6895 | +                 includes=[test_file('third_party/openjpeg/libopenjpeg'),  | 
 | 6896 | +                           test_file('third_party/openjpeg/codec'),  | 
 | 6897 | +                           test_file('third_party/openjpeg/common'),  | 
 | 6898 | +                           Path(self.get_build_dir(), 'third_party/openjpeg')],  | 
 | 6899 | +                 output_nicerizer=image_compare)  | 
6921 | 6900 | 
 
  | 
6922 | 6901 |   @also_with_standalone_wasm(impure=True)  | 
6923 | 6902 |   @no_asan('autodebug logging interferes with asan')  | 
 | 
0 commit comments