Adding mask to input image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
srinath129
Posts: 1
Joined: 2015-08-31T12:34:52-07:00
Authentication code: 1151

Adding mask to input image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding mask to input image

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding mask to input image

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding mask to input image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding mask to input image

Post by snibgo »

Another way is to divide by 4 then multiply by 4. This won't work with HDRI.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Adding mask to input image

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Adding mask to input image

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply