Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profusion-mobi.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=y2tmsgxxUHp/tWqXdxDjhk1Y+5WaEqhmoOek5Oku8XM=; b=p8UWZDTLzpBvxWnxiRUckjukhuZ5kyxM5f4zXpjxKOlkEKbjYlLmAFmhv0skshQSNs D0RZhFC3NLwvV6wxDep5evdua4XMiuCYIy17LqH7qR3U92jU6E1x/+CmhR51vHpuMcXz oWl+P6ZTnxSWI53bYADfkILsMG0EbSW6v2C3BGhFBpOLRPJhhzinndOX16W/uezjcuNw AZU6SzO2n8NOfXdmEycHaq9dngXn+XpxBs82AjK6+FDkOzBTehB/EkFRaCn5y3oaeZAy UBuT7QOA9JVR9NrlZCBQXriLeb6tD16Lk2BQ3B/JuRpYr9KOcRM6DxX+36Z+YBIJQS02 vKBg== From: Guilherme Campos Camargo Date: Tue, 24 Apr 2018 14:24:49 -0300 Message-Id: <20180424172451.2682-13-guicc@profusion.mobi> In-Reply-To: <20180424172451.2682-1-guicc@profusion.mobi> References: <20180424172451.2682-1-guicc@profusion.mobi> Subject: [Fuego] [PATCH 12/14] Allow compare-with-mask to work with ImageMagick 6 and ImageMagick 7 List-Id: Mailing list for the Fuego test framework List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: fuego@lists.linuxfoundation.org The compare command on ImageMagick 7+ supports the --read-mask modifier, that simplifies the comparison by allowing an ignore-mask to be provided. ImageMagick 6 does not support that option, so in order to ignore regions we need to compose two intermediate images, from the reference and from the test, in which the ignored regions are painted black. On this patch, we're implementing that logic, allowing Magick 7 and Magick 6 to be used. The reason why we're allowing Magick 6 to be used is because Magick 7 is not yet officially available neither on Jessie nor on Stretch, and if we wanted to install it from source on Fuego, we would need to upgrade a few dependencies (as libc) what could possibly end up causing negative side effects. On the other hand we're not dropping completely ImageMagick 7 implementation because users may want to execute the test outside Fuego (as explained on the README), taking advantage of the new features if ImageMagick 7 is installed in their system. Signed-off-by: Guilherme Campos Camargo --- .../Functional.fuego_release_test/test_run.py | 104 ++++++++++++++---- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/engine/tests/Functional.fuego_release_test/test_run.py b/engine/tests/Functional.fuego_release_test/test_run.py index 47829f3..a36014d 100755 --- a/engine/tests/Functional.fuego_release_test/test_run.py +++ b/engine/tests/Functional.fuego_release_test/test_run.py @@ -183,10 +183,44 @@ class CheckScreenshot(SeleniumCommand): self.test_img_path = add_suffix(ref_img, 'test') - def compare_images(self, current_img_path, reference_img_path, - mask_img_path, diff_img_path, threshold): - cmd = ('magick', - 'compare', + def compare_cmd_magick_v7(test_img, ref_img, mask_img, diff_img): + """ + This compare command uses the --read-mask option that works only with + ImagMagick V7.0.3.9+. That option allows a mask to be used for limiting + the regions to be compared. + """ + cmd = ('compare', + '-verbose', + '-metric', + 'RMSE', + '-highlight-color', + 'Red', + '-compose', + 'Src', + '-read-mask' if mask_img else None, + mask_img if mask_img else None, + test_img, + ref_img, + diff_img, + ) + return ' '.join(list(filter(None, cmd))) + + def compare_cmd_magick_v6(test_img, ref_img, mask_img, diff_img): + """ + This compare command does not use the option --read-mask that is + available only on ImageMagic V7.0.3.9+. It has been tested with + ImageMagick V6.0 and works fine. The strategy being used here is to + multiply test and ref by the same B/W mask, in which black regions will + be ignored during the comparison. + """ + def apply_mask_cmd(img, mask=None): + if not mask: + return (img,) + + return ( + '<(composite -compose Multiply %s %s png:)' % (img, mask),) + + cmd = ('compare', '-verbose', '-metric', 'RMSE', @@ -194,28 +228,56 @@ class CheckScreenshot(SeleniumCommand): 'Red', '-compose', 'Src', - '-read-mask' if mask_img_path else None, - mask_img_path if mask_img_path else None, - current_img_path, - reference_img_path, - diff_img_path, ) + cmd += apply_mask_cmd(test_img, mask_img) + cmd += apply_mask_cmd(ref_img, mask_img) + cmd += (diff_img,) - cmd = list(filter(None, cmd)) + return ' '.join(list(cmd)) - LOGGER.debug(' Comparing images...') - LOGGER.debug(' cmd: $ %s', ' '.join(cmd)) - process = subprocess.Popen(cmd, universal_newlines=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - process_stdout, process_stderr = process.communicate() - LOGGER.debug(' Results:') - LOGGER.debug(' return code: %s', process.returncode) - LOGGER.debug(' stdout: %s', process_stdout.strip()) - LOGGER.debug(' stderr: %s', process_stderr.strip()) + def compare_images(self, current_img_path, reference_img_path, + mask_img_path, diff_img_path, threshold): + def exec_cmd(cmd): + LOGGER.debug(' cmd: $ %s', cmd) + process = subprocess.Popen(cmd, universal_newlines=True, + executable='/bin/bash', shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + process_stdout, process_stderr = process.communicate() + LOGGER.debug(' Results:') + LOGGER.debug(' return code: %s', process.returncode) + LOGGER.debug(' stdout: %s', process_stdout.strip()) + LOGGER.debug(' stderr: %s', process_stderr.strip()) + + return (process.returncode, process_stdout, process_stderr,) + + def cmd_failed(ret): + """ + ImageMagick Compare command returns 2 if the command failed, 1 if + the images are dissimilar and 0 if the images are similar. This + function checks if the return code represents an error, returning + True if so. + """ + MAGICK_CMP_ERROR = 2 + return ret == MAGICK_CMP_ERROR + + LOGGER.debug(' Comparing images with ImageMagick v7 API...') + ret, stdout, stderr = exec_cmd( + CheckScreenshot.compare_cmd_magick_v7(current_img_path, + reference_img_path, + mask_img_path, + diff_img_path)) + + if cmd_failed(ret): + LOGGER.debug(' Comparing images with ImageMagick v6 API...') + ret, stdout, stderr = exec_cmd( + CheckScreenshot.compare_cmd_magick_v6(current_img_path, + reference_img_path, + mask_img_path, + diff_img_path)) RESULTS_REGEX = re.compile(".*all:.*\(([0-9\-\.e]*)\).*") - result = RESULTS_REGEX.search(process_stderr) + result = RESULTS_REGEX.search(stderr) if not result: LOGGER.error(" Error processing the output.") -- 2.17.0