Jump to content

MathMagick


Werty
 Share

Recommended Posts

Found a way to use ImageMagick for maths using the FX function.

$img = ObjCreate("ImageMagickObject.MagickImage.1")
$result1 = $img.Convert("null:","-format","%[fx:1+1]","-:")
MsgBox (0, "Result: ", $result1)
$result2 = $img.Convert("null:","-format","%[fx:4*atan(1)*cosh(12)*4+45^2]","-:")
MsgBox (0, "Result: ", $result2)

As ImageMagick is multithreaded via OpenMP this just may be also, but I'm not much into maths so dunno how to test that, I tried some for-next loops and got my quadcore above 25% usage, but no more than 27-29% cpu usage, maybe some of you math guru's can test it out with some complicated formulas.

Anyways, it's awfully slow compared to AutoIt, as it calls the IM dll every time, the math itself may be faster though, dunno for sure, please test.

List of expressions...

•x ^ y: exponentiation (xy)

•( ... ): grouping

•x * y: multiplication (the asterisk * is optional, for example, 2u or 2(x+y) are acceptable)

•x / y: division

•x % y: modulo

•x + y: addition

•x - y: subtraction

•x << y: left shift

•x >> y: right shift

•x < y: boolean relation, return value 1.0 if x < y, otherwise 0.0

•x <= y: boolean relation, return value 1.0 if x <= y, otherwise 0.0

•x > y: boolean relation, return value 1.0 if x > y, otherwise 0.0

•x >= y: boolean relation, return value 1.0 if x >= y, otherwise 0.0

•x == y: boolean relation, return value 1.0 if x == y, otherwise 0.0

•x != y: boolean relation, return value 1.0 if x != y, otherwise 0.0

•x & y: binary AND

•x | y: binary OR

•x && y: logical AND connective, return value 1.0 if x > 0 and y > 0, otherwise 0.0

•x || y: logical OR connective (inclusive), return value 1.0 if x > 0 or y > 0 (or both), otherwise 0.0

•~x: logical NOT operator, return value 1.0 if not x > 0, otherwise 0.0

•+x: unary plus, return 1.0*value

•-x: unary minus, return -1.0*value

•x ? y : z: ternary conditional expression, return value y if x != 0, otherwise z; only one ternary conditional permitted per statement

•x = y: assignment; assignment variables are restricted to letter combinations only (e.g. Xi not X1)

•x ; y: statement separator

•pi: constant (3.141659...)

•e: constant (2.71828...)

•QuantumRange: constant maximum pixel value (255 for Q8, 65535 for Q16)

•QuantumScale: constant 1.0/QuantumRange

•intensity: pixel intensity; equivalent to 0.299*red+0.587*green+0.114*blue

•hue: pixel hue

•saturation: pixel saturation

•lightness: pixel lightness; equivalent to 0.5*max(red,green,blue) + 0.5*min(red,green,blue)

•luminance: pixel luminance; equivalent to 0.2126*red + 0.7152*green + 0.0722*blue

•red, green, blue, etc.: color names

•#ccc, #cbfed0, #b9e1cc00, etc.: color hex values

•rgb(), rgba(), cmyk(), cmyka(), hsl(), hsla(): color functions

•s, t, u, v, n, i, j, w, h, z, r, g, b, a, o, c, y, m, k: symbols

•abs(x): absolute value function

•acos(x): arc cosine function

•airy(x): Airy function (max=1, min=0); airy(x)=[jinc(x)]2=[2*j1(pi*x)/(pi*x)]2

•alt(x): sign alternation function (return 1.0 if int(x) is even, -1.0 if int(x) is odd)

•asin(x): arc sine function

•atan(x): arc tangent function

•atan2(y,x): arc tangent function of two variables

•ceil(x): smallest integral value not less than argument

•channel(r,g,b,a): select numeric argument based on current channel

•channel(c,m,y,k,a): select numeric argument based on current channel

•cos(x): cosine function

•cosh(x): hyperbolic cosine function

•debug(x): print x (useful for debugging your expression)

•exp(x): natural exponential function (ex)

•floor(x): largest integral value not greater than argument

•hypot(x,y): the square root of x2+y2

•int(x): greatest integer function (return greatest integer less than or equal to x)

•j0(x): Bessel functions of x of the first kind of order 0

•j1(x): Bessel functions of x of the first kind of order 1

•jinc(x): jinc function (max=1, min=-0.1323); jinc(x)=2*j1(pi*x)/(pi*x)

•ln(x): natural logarithm function

•log(x): logarithm base 10

•logtwo(x): logarithm base 2

•ln(x): natural logarithm

•max(x, y): maximum of x and y

•min(x, y): minimum of x and y

•mod(x, y): floating-point remainder function

•pow(x,y): power function (xy)

•rand(): value uniformly distributed over the interval [0.0, 1.0) with a 2 to the 128th-1 period

•round(): round to integral value, regardless of rounding direction

•sign(x): return -1.0 if x is less than 0.0 otherwise 1.0

•sin(x): sine function

•sinc(x): sinc function (max=1, min=-0.21); sinc(x)=sin(pi*x)/(pi*x)

•sinh(x): hyperbolic sine function

•sqrt(x): square root function

•tan(x): tangent function

•tanh(x): hyperbolic tangent function

•trunc(x): round to integer, towards zero

The expression semantics include these rules:

•symbols are case insensitive

•only one ternary conditional (e.g. x ? y : z) per statement

•statements are assignments or the final expression to return

•an assignment starts a statement, it is not an operator

•assignments to built-ins do not throw an exception and have no effect; e.g. r=3.0; r returns the pixel red color value, not 3.0

•Unary operators have a lower priority than binary operators, that is, the unary minus (negation) has lower precedence than exponentiation, so -3^2 is interpreted as -(3^2) = -9. Use parentheses to clarify your intent (e.g. (-3)^2 = 9).

•Similarly, care must be exercised when using the slash ('/') symbol. The string of characters 1/2x is interpreted as (1/2)x. The contrary interpretation should be written explicitly as 1/(2x). Again, the use of parentheses helps clarify the meaning and should be used whenever there is any chance of misinterpretation.

Read more about IM FX here.

Dunno how usefull this could be. but atleast it has some math functions that AutoIt hasnt.

Edited by Werty

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...