Page 1 of 1

Adding mask to input image

Posted: 2015-08-31T12:39:52-07:00
by srinath129
Hi all,

Need to create a custom mask to an input tiff file, was wondering if anyone can start me off in the right direction on how to do so.

I have an 48RGB input tiff file (16 bits/channel), and need to mask out the lowest two bits, bits 0 and 1. (Basically am down-sampling to 14-bits/channel, but need to preserve in 16bits range). Is there an option already in ImageMagick to do this, or how can I create a custom mask that does this? The resolution of my input images can be variable

Thanks,
Srinath

Re: Adding mask to input image

Posted: 2015-08-31T12:53:14-07:00
by fmw42
2^14 - 1 = 16383

So this, I think, will work. It will scale all values from 2^16 - 1 (full 16-bit white) to 2^14 - 1 and the rest proportionally.

Code: Select all

convert image +level 0x16383 result

Re: Adding mask to input image

Posted: 2015-08-31T13:00:27-07:00
by snibgo
I don't think that's what the OP wants to do.

To zero the bottom two bits, you can "AND" with a number that is all binary one's but with zeros in the bottom two bits. In Q16, that number is 65535-3 = 65532.

Code: Select all

convert in.png -evaluate And 65532 out.png
An alternative is to shift bits:

Code: Select all

convert in.png -evaluate RightShift 2 -evaluate LeftShift 2 out.png

Re: Adding mask to input image

Posted: 2015-08-31T14:40:41-07:00
by fmw42
snibgo wrote:I don't think that's what the OP wants to do.
Yes, I think you are right. I thought about the bit shift subsequently at lunch, but I did not know how to do that correctly.

Re: Adding mask to input image

Posted: 2015-08-31T15:51:41-07:00
by snibgo
Another way is to divide by 4 then multiply by 4. This won't work with HDRI.

Re: Adding mask to input image

Posted: 2015-08-31T16:10:59-07:00
by fmw42
snibgo wrote:Another way is to divide by 4 then multiply by 4. This won't work with HDRI.
Could you not divide by 4, -clamp, then multiply by 4 in HDRI?

Re: Adding mask to input image

Posted: 2015-08-31T16:30:42-07:00
by snibgo
Clamp merely restricts the values to 0 to 100%, which they would be anyway. I don't think there is a simple supported way to "remove the fractional part" of HDRI values.

There is a way that works: OR with zero. It is a quirk so I wouldn't rely on this.

Code: Select all

-evaluate divide 4 -evaluate Or 0 -evaluate Multiply 4
I suppose the bitwise operators AND, OR etc first convert values to integers.