Clipping Color Range via CLUT

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
sunape
Posts: 1
Joined: 2013-02-28T06:50:40-07:00
Authentication code: 6789

Clipping Color Range via CLUT

Post by sunape »

Hi everyone,

This is what I'm trying to do:
For each color channel in an 24bit RGB image, replace all values below 16 with 16 and all values above 235 with 235.

What I tried:
I made a color lookup table ("clut.pgm", one row, 256 values). No values under 16 or above 235 are included in the table, all other values are sequential (example 8->16, 21->21, 45->45, 240->235).

I ran it through convert:

Code: Select all

convert 01429_sunsetinedinburgh_1280x800.jpg clut.pgm -clut output.png
Now, when I'm checking the histogram of output.png, I still see a lot of color values < 16. If I run -clut twice, though, these stray pixel values are gone.

Code: Select all

convert 01429_sunsetinedinburgh_1280x800.jpg clut.pgm -clut clut.pgm -clut output.png
Now, I do need a 100% surefire way to eliminate all values below 16 and above 235, so that second option is not good either (what if, for some images, I need to run it three times?).

Is this a bug? It has nothing to do with input/output formats (I tried). Any other ways to do with I'm trying to do?

EDIT: Sample data is available here.


EDIT2: ... and I found the solution myself. My CLUT was wrong (0x0D 0x0A in PGM instead of 0x0D) :-/. Duh... Can any moderator delete this thread?
..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clipping Color Range via CLUT

Post by snibgo »

What IM version? What platform?

So, clut.pgm contains 256 values. The first 16 values are 16, the last 21 are 235, and values 17 to 234 are set to 17 to 234. That should work. You can verify the values are correct:

Code: Select all

convert clut.pgm txt:
There are other ways to achieve this, eg with threshold, then turning black or white to the apropriate shade of gray. I would expect clut to be faster.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Clipping Color Range via CLUT

Post by anthony »

Use a -level and +level, to clip the color range...

See these effects in the example on "burning and clipping"
http://www.imagemagick.org/Usage/basics ... um_effects

Warning IMv7 is HDRI by default, so you will need to include a "-clamp" operation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply