see my unix bash script, plm at the link below
Note that for Q8 or Q16 IM compiles, grayscale values in IM are, repsectively, integers 0-255 or 0-65535. You can also use floats 0-100%.
The other way you can try to do it is using a set of -fx ternary conditional commands or possibly one long set of nested ternary conditional commands in one -fx statement. see http://www.imagemagick.org/script/fx.php
Note within -fx, graylevels are in the range of floats 0 to 1.
In order to use floating point Tiffs you must compile IM in HDRI mode; otherwise your tif will get truncated to integer values in whatever Q level you compile IM. see http://www.imagemagick.org/Usage/basics/#depth
and http://www.imagemagick.org/script/high- ... -range.php