From 5a028773bff7100224b4d881f5bce3da4d430a3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Tschumperl=C3=A9?= <David.Tschumperle@gmail.com>
Date: Tue, 25 Sep 2018 21:47:39 +0200
Subject: [PATCH] _math_parser() : Improve behavior of swap() when source or
 destination are reference to vector-valued pixels.

---
 CImg.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/CImg.h b/CImg.h
index 54da20a..74e2f7e 100644
--- a/CImg.h
+++ b/CImg.h
@@ -19119,7 +19119,7 @@ namespace cimg_library_suffixed {
               arg1 = p1 = compile(ss5,s1,depth1,ref,is_single);
               s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
               arg2 = compile(s1,s2,depth1,ref._data + 7,is_single);
-              arg3 = ~0U; arg4 = arg5 = 1;
+              arg3 = arg4 = arg5 = ~0U;
               if (s2<se1) {
                 s3 = ++s2; while (s3<se1 && (*s3!=',' || level[s3 - expr._data]!=clevel1)) ++s3;
                 arg3 = compile(s2,s3,depth1,0,is_single);
@@ -19129,12 +19129,18 @@ namespace cimg_library_suffixed {
                   arg5 = s1<se1?compile(++s1,se1,depth1,0,is_single):1;
                 }
               }
-              if (_cimg_mp_is_vector(arg1) && !ref[0]) ++arg1;
+              if (_cimg_mp_is_vector(arg1)) {
+                if (!ref[0]) ++arg1;
+                else if (ref[0]>=4 && arg4==~0U) arg4 = scalar1(mp_image_whd,ref[1]);
+              }
               if (_cimg_mp_is_vector(arg2)) {
                 if (arg3==~0U) arg3 = constant(_cimg_mp_size(arg2));
                 if (!ref[7]) ++arg2;
+                if (ref[7]>=4 && arg5==~0U) arg5 = scalar1(mp_image_whd,ref[8]);
               }
               if (arg3==~0U) arg3 = 1;
+              if (arg4==~0U) arg4 = 1;
+              if (arg5==~0U) arg5 = 1;
               _cimg_mp_check_type(arg3,3,1,0);
               _cimg_mp_check_type(arg4,4,1,0);
               _cimg_mp_check_type(arg5,5,1,0);
-- 
GitLab