Merging 2+ PAM GRAYSCALE_ALPHA images

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
PatrickPhotog
Posts: 7
Joined: 2020-02-04T12:06:49-07:00
Authentication code: 1152

Merging 2+ PAM GRAYSCALE_ALPHA images

Post by PatrickPhotog »

I am trying to merge 2 PAM, P7 grayscale/alpha images (TUPLTYPE GRAYSCALE_ALPHA, uint16[2] ->uint32_t per pixel).
The result comes out in "TUPLTYPE RGB_ALPHA".

Code: Select all

convert pf-269361.red.sp0.5.m1.16.pam  pf-269369.red.sp0.5.m1.16.pam -compose CopyOpacity  -composite  pf-269361_9.red.sp0.5.m1.16.im.pam
head -n7   pf-269361.red.sp0.5.m1.16.pam   pf-269361_9.red.sp0.5.m1.16.im.pam | egrep -i "pam|tuplt"
pf-269361.red.sp0.5.m1.16.pam          TUPLTYPE GRAYSCALE_ALPHA    [16 bits GRAY, 16 bits OPACity]] 
pf-269361_9.red.sp0.5.m1.16.im.pam  TUPLTYPE RGB_ALPHA                 [ 16 bits red, green, blue and opacity ]
The output of `identify -verbose` lists both grayscale and trueColor sRGB:
Colorspace: sRGB
Type: GrayscaleAlpha
Base type: TrueColor < GrayScale and RGB completely different datatypes? Uint16 vs Uint[3][16] or ~Uint48?
Channel depth:
Red: 16-bit; Green: 16-bit; Blue: 16-bit; Alpha: 16-bit

And the RGB channels all have the same min/max/mean/sdev/kurt/skew/entr so the image would look gray.

My goal is to do something equivalent to the Photoshop Operations:
  • Open PAM1
    Select_all
    Copy
    Open PAM2
    Create New Layer
    Paste PAM1 on a layer above Pam2
    Layer->Merge
    Save_as PAM3 (as 16_bit_grayscale with 16_bit_alpha)
How does one tell ImageMagick to not change Grayscale to RGB while keeping the Alpha?

OpenSuse: ImageMagick 7.0.8-20 Q16 x86_64 2018-12-25 https://imagemagick.org (latest available)

snibgo
Posts: 12936
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merging 2+ PAM GRAYSCALE_ALPHA images

Post by snibgo »

For v7, I suggest you use "magick", not "convert".

Try inserting "-type grayscalealpha" before writing the output pam.
snibgo's IM pages: im.snibgo.com

PatrickPhotog
Posts: 7
Joined: 2020-02-04T12:06:49-07:00
Authentication code: 1152

Re: Merging 2+ PAM GRAYSCALE_ALPHA images

Post by PatrickPhotog »

Convert is linked to magick on my system.

The "-type grayscalealpha" did give the right format, 16 bits of Gray and 16 bits of Opacity.

Getting rid if the "-compose CopyOpacity " computes the opacity fall-through correctly.

Code: Select all

/usr/local/bin/magick -verbose pf-269369.red.half.opac.pam  pf-269369.red.half.opac.pam   -composite -type grayscalealpha pf-269369.red.0.75.opac.im4.pam
pf-269369.red.half.opac.pam PAM 7360x4912 7360x4912+0+0 16-bit Grayscale Gray 137.91MiB 0.520u 0:00.140
pf-269369.red.half.opac.pam PAM 7360x4912 7360x4912+0+0 16-bit Grayscale Gray 137.91MiB 0.540u 0:00.149
pf-269369.red.half.opac.pam=>pf-269369.red.0.75.opac.im4.pam \
    PAM 7360x4912 7360x4912+0+0 16-bit GrayscaleAlpha Gray 137.91MiB 4.330u 0:01.129
The 2 input files are the same file with 50% opacity. The resulting file has 75% opacity; lets half of half of the light fall through.

It agrees exactly to the MD5sum level with a Perl version.

snibgo
Posts: 12936
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merging 2+ PAM GRAYSCALE_ALPHA images

Post by snibgo »

PatrickPhotog wrote:Convert is linked to magick on my system.
Nevertheless, they probably do different things. The program figures out what command invoked it, and assumes the command syntax is v6 for "convert" or v7 for "magick".
PatrickPhotog wrote:The 2 input files are the same file with 50% opacity. The resulting file has 75% opacity; lets half of half of the light fall through.
As expected, for "-compose Over" (which is the default). It's like looking through two sheets of semi-transparent paper. Two sheets together pass less light than either alone.
snibgo's IM pages: im.snibgo.com

PatrickPhotog
Posts: 7
Joined: 2020-02-04T12:06:49-07:00
Authentication code: 1152

Re: Merging 2+ PAM GRAYSCALE_ALPHA images

Post by PatrickPhotog »

<< command syntax is v6 for "convert" or v7 for "magick".
The old argv[0] peek trick! When is a ln not a ln? Slick!

I found this on the web (sorry didn't annotate location)
C is Color (gray value) and O is Opacity, both in range [0 .. 65535]
Semi-transparent pixels 1 and 2 are merged to form pixel 3
As noted, " It's like looking through two sheets of semi-transparent paper."

I tested it with uint16_t[2] quanta, 16 bits of gray and 16 opacity

Code: Select all

	# C3 = C1 * O1 + C2 * O2 * (1 - O1)
	# O3 = O1 + O2 * (1 - O1)
	# C3 /= O3
It agrees exactly with the magick -composite ... command

gra_alp_merge.pl 12345 32768 12345 32768 <enter>
G=12345, O=49152 << 32768 * 1.5 = 49152, 75% opacity. Check!

========================
Gory Opacity details:

How many layers at 1/2 opacity does it take to merge to exactly 100% opacity with 16 bit resolution?
17

Running 16 leaves 1 part in 2^16 as pass through or OPAC=65534:

Code: Select all

Running gra_alp_merge.pl  Fri Feb  7 12:28:24 2020
II=0, g=12345, o=32768, G=0.188372625, O=0.500007630, vb=0.500007630, bs=0.500007630, gs=0.0941877498, vi= 0.49999237
II=1, g=12345, o=32768, G=0.188372625, O=0.500007630, vb=0.250000000, bs=0.750007629, gs=0.141280906, vi=0.249992371
<SNIP>
II=14, g=12345, o=32768, G=0.188372625, O=0.500007630, vb=0.000030512, bs=0.999969489, gs=0.188366878, vi=3.05105938e-05
II=15, g=12345, o=32768, G=0.188372625, O=0.500007630, vb=0.000015256, bs=0.999984745, gs=0.188369752, vi=1.52550641e-05
G=12345, O=65534
Add 1 more layer for 17 total, and it rounds to a perfect 65535 (with 15 sig fig Double math)
...

Code: Select all

II=16, g=12345, o=32768, G=0.188372625, O=0.500007630, vb=0.000007628, bs=0.999992373, gs=0.188371189, vi=7.62741568e-06
G=12345,  O=65535  << Perfect Score!
Elapsed time = 0.232 ms   << FAST for 1 pixel, 2+ hours for a whole picture!

snibgo
Posts: 12936
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Merging 2+ PAM GRAYSCALE_ALPHA images

Post by snibgo »

The authority for IM's compositing is https://www.w3.org/TR/SVGCompositing/ . I have summarised the formulae in Compose operators de-mystified. Or perhaps mystified even further.
snibgo's IM pages: im.snibgo.com

Post Reply