PGM Colourisation

Stuart McDonagh via Graphicsmagick-help <[email protected]> Mon, 14 Dec 2020 22:16:49 +0100 (CET)
Newsgroups gmane.comp.video.graphicsmagick.help
Message-ID <[email protected]>
Hello,

I am having trouble understanding why performing a particular operation in specific circumstances generates what I perceive to be erroneous output.

So I am producing a visualisation of a spectrum analyser.
My program produces frames in PGM (Portable Graymap) binary format, because it is simple to implement.
These frames contain a maximum of 4 shades of grey:-
#000000 (black) - the background;
#555555 - low-value peaks;
#AAAAAA - mid-value peaks;
#FFFFFF max-value peaks.

Afterwards, I open each frame in GraphicsMagick, like so:-
gm convert -depth 8 test00000.pgm -fill green -opaque '#555555' -fill yellow -opaque '#AAAAAA' -fill red -opaque '#FFFFFF' -type palette test00000.png

The idea is to convert the greyscale image to colour, with the peaks represented by green, yellow and red respectively, then losslessly compress.

The 'depth' argument is due to the packaged gm being built as Q16; output files will by default contain 16-bit samples.
The 'type' argument reduces the disk space being used for these simple images.

However, it doesn't seem to work properly, instead the background (black) gets changed to green.
The peaks do not change colour at all.
In addition, I get the error message "Unrecognized color #FFFFFF".

Trying to debug this, I think that in the "gray" colourspace, the lowest colour in the palette is "black" and the highest is "white", with some shades of grey having specific names and the rest being identified by their hex values.
If a palette value is named, I can't use its hex value?

I have read the documentation for the "-opaque" command several times and it indicates that it should match the exact colour, or nearest if there is a fuzz applied (defaults to 0).
Therefore, I don't understand why trying to fill '#555555' with green results in black being changed.

Do I need to convert the colourspace before I can use actual colours?  How do I do that?

gm version
GraphicsMagick 1.3.34 2019-12-24 Q16 http://www.GraphicsMagick.org/
Copyright (C) 2002-2019 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.

Feature Support:
  Native Thread Safe       yes
  Large Files (> 32 bit)   yes
  Large Memory (> 32 bit)  yes
  BZIP                     yes
  DPS                      no
  FlashPix                 no
  FreeType                 yes
  Ghostscript (Library)    no
  JBIG                     yes
  JPEG-2000                yes
  JPEG                     yes
  Little CMS               yes
  Loadable Modules         yes
  Solaris mtmalloc         no
  OpenMP                   yes (201511 "4.5")
  PNG                      yes
  TIFF                     yes
  TRIO                     no
  Solaris umem             no
  WebP                     yes
  WMF                      yes
  X11                      yes
  XML                      yes
  ZLIB                     yes

Host type: x86_64-redhat-linux-gnu

Regards

--------
Stuart