Greetings, and welcome back. I remember our previous disscussions about generating tempreture maps.
Obviously these are not tempreture maps seeing as you are now wanting
discontinuous color ranges.
Basically by 'discontinous' I mean... In the 'PGM' image you have two values side-by-size 3 5 But the resize and CLUT is adding a small gradient between these two values and thus producing a color range value of 4 color between squares 3 and 5. You do not want a gradient, or continuious range of value, but a discontinuation.
The problem here is NOT the CLUT operation. That is just substituting values as expected. The problem is when you resize, which generates that gradient between the two values.
Resize is not the answer. What you want is a resize that adds no new colors (values) between points.
Solution 1: Use -scale or -sample (either will do for enlargements).
This will generate 'pixelated' squares of color, without generating any intervening colours between them.
This is also what you would get if you had simply used a -sparse-color volonoi on single points in the middle of each square. Which if I remember you were trying to do at tthe start of our previous discussion (email)
Obviously this looks horrible, and was presumably already rejected as a solution.
Solution 2: Generate a separate mask of each color and merge the masks.
That is one mask of all '5' values only, enlarge and threshold and edge smooth, that mask (say at 50%), after that color that mask to be the '5' color and transparency. Now overlay (flatten) all masks together.
This was looked at extensively in a older discussion topic on generating tiled maps of landscapes (for a game)
viewtopic.php?f=1&t=14250
The problem is that the masks may not cover the whole image, but leave small 'holes' or gaps between the different colors. If you are using tiles you could generate a blur overlap between tiling images, but you specified you want colors not tiles, and blur overlap would just result in your previous problem.
Something that wasn't available during that discussion is to 'hole fill' using a Voronoi method. See
Shepards Sparse Hole Fill but use Voronoi instead of
Shepards. It is not particularly nice as you need to extract all edge pixels and convert them into a color point list.
if you have the very latest Imagemagick a NEW and fast direct alternative is...
convert image_with_holes.png -morphology Voronoi Octagon holes_filled.png
WARNING: the above is not 'offical' yet, and is only an experimental addition which will likely be called from some other Image processing option instead of morphology (which only provides the distance function it uses internally). It is a step for a much more advanced hole-filly method (Diffusion color fills as aposed to problems with a Shepards Color Fills, both related but differs in boundary handling)
Solution 3: Generate a solution 1, then look for and round off corners.
This was also suggested in the discussion for the previous solution, but difficult to implement. It also still generates a fairly squareish look at 3 color joins. However in may ways it is similar to (a more limited) general solution (see next)
Solution 4: Use a technique developed for video games known as
Pixel scaling.
See
http://en.wikipedia.org/wiki/Pixel_art_ ... algorithms
This has not been implemented in ImageMagick as yet, and won't be until we get some programmer willing (and has time) to do the work. I would love to see it added, and even have it on my ToDo but I have too many other To Do's that I never seem to get to before it.
Some algorithms for this include...
Scale2x
http://scale2x.sourceforge.net/
HQ3x
http://www.hiend3d.com/hq3x.html
You may also be interested in a related topic, for rotating without adding extra colors
http://info.sonicretro.org/RotSprite