GreycLab / CImg

The CImg Library is a small and open-source C++ toolkit for image processing
http://cimg.eu
Other
1.48k stars 282 forks source link

Add concat function (WIP) #338

Closed Reptorian1125 closed 2 years ago

Reptorian1125 commented 2 years ago

It doesn't seem discoverable by g'mic cli. I'm also not expecting mp_concat to work due to very little experience with C++. This is more of a starting point.

Reptorian1125 commented 2 years ago

Here's the output so far:

c:\gmic-devel\gmic\src>gmic echo {concat(-50,20,30,.09)}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ -502030.09000000003
[gmic]-0./ End G'MIC interpreter.

c:\gmic-devel\gmic\src>gmic echo {concat(5,20,30,.09)}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ 52030.089999999997
[gmic]-0./ End G'MIC interpreter.

c:\gmic-devel\gmic\src>gmic echo {concat(5,20,30,8)}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ 520308
[gmic]-0./ End G'MIC interpreter.

For first one, since the last argument doesn't have a 0 at the beginning, a unnecessary 0 is added. But, I believe users can address that with ease.

dtschump commented 2 years ago

Won't consider this for merging, because:

Reptorian1125 commented 2 years ago

This functions seems rather useless by itself. Maybe you need it for a certain filter, but for general use, concatenate numbers is not a common operation, particularly when it is already possible to concatenate strings.

It's not common, yes, I agree. However, this is easier to work with when using math evaluation within G'MIC, which is exactly why I have created it in the first place. It also enables access to this function within filters that supports custom expression, which is another reason why I did this. This doesn't involve strings. One can do something like this in G'MIC with some time though with the setback of having to copy and paste this function.

Moreover, this won't work for large numbers. Numbers are stored in double, and there is no way this concat() function can work with any number of arguments.

Yes, large numbers won't work with it, I agree with that. It does work for any number of arguments from 1-n where the concatenated number is less than DBL_MAX.

That being said, at the end of the day, I respect your decision.

dtschump commented 2 years ago

Also, there is a way to do this directly with macros :

foo :
  eval "
    nbd(x) = x>0?1+int(log10(x)):1;
    concat(a,b) = (a*10^nbd(b) + b);
    concat(a,b,c) = (concat(a,b)*10^nbd(c) + c);
    concat(a,b,c,d) = (concat(a,b,c)*10^nbd(d) + d);
    concat(a,b,c,d,e) = (concat(a,b,c,d)*10^nbd(e) + e);

    print(concat(100,50,25,12,7));
  "
Reptorian1125 commented 2 years ago

I did not realize that functions with same name can have different numbers of arguments at all. Thanks.

Reptorian1125 commented 2 years ago

I may have prematurely closed this.

After trying to make own macro generator within G'MIC. I realized that my concat() function in this pull request actually factors into conditions such as whether the first argument is a positive or a negative, and whether every arguments except the last one contains a decimal. It doesn't look very feasible to do this in G'MIC language. My solution solves most of the shortcomings with the G'MIC macro.

Maybe, I'll have another crack, but I find it very difficult to work without resorting to math-based approach. Maybe it's not worth reopening.