View Single Post
Old 10th November 2015, 02:16   #1  |  Link
jmac698
Registered User
 
Join Date: Jan 2006
Posts: 1,867
Image Magick and stacked high bit-depth

In case this is useful to anyone, after a few hours of playing around thanks to poor documentation, here's how to convert to/from 16bit PNG images and Avisynth convention stacked high/low formats. This works with the 16bit version of imagemagick. Note that Linux users will have to escape the parenthesis.
http://www.imagemagick.org/script/bi...es.php#windows
Need ImageMagick-6.9.2-5-Q16-x64 version

convert 16bit to stacked (high/low):
Code:
convert 16bit.png -depth 16 ( +clone -evaluate and 255 -evaluate multiply 256 ) -append stacked.png
convert stacked (high/low) to 16bit:
Code:
convert stacked.png -depth 16 -crop 100%x50% ( +clone -evaluate divide 256 ) -delete 1 -compose Add -composite -define png:bit-depth=16 16bit1.png
I should add how this works.
Code:
convert source options output
Code:
-depth 16
Indicate to use 16bit processing.
Code:
-crop 100%x50%
Crops to 100% of the width but 50% of the height. Places this image on an internal stack. For some reason, it also places the left over image (the bottom half) on the stack as well.
Code:
( +clone -evaluate divide 256 )
The parenthesis indicates a new sub-processing stream. +clone repeats the last image on the stack and becomes the source for this subprocessing (in this case the bottom half).
-evaluate performs a mathematical operation on the image. Since we've set bit depth to 16, think in terms of 16bit numbers. An 8bit image is convert to 16bit by making it the high byte. Therefore, we need to add the top half (as 0xHH00) to the bottom half, shifted right by 8, or divided by 256.
The new image is placed on the stack (as 0x00LL)
Code:
-delete 1
This deletes item 1 on the stack, counting from 0, so the middle image. Item 0 is the top half, item 1 is the bottom half, and item 2 is the bit-shifted bottom half. Now there's just the top half and the bit-shifted bottom half.
Code:
-compose Add -composite
The compose command sets the composition type. The composite command is like overlay. Add will add the previous two images together and put the result on the stack. This is then the final 16bit image.
Code:
-define png:bit-depth=16
This sets the option to make png's 16bit.
Code:
16bit.png
This is the output filename and fileformat for the final image. It writes the last entry on the stack.

The inverse process should be easy to follow now.
Code:
convert 16bit.png -depth 16 ( +clone -evaluate and 255 -evaluate multiply 256 )
Takes a source, use 16bit processing, copy the source to a new stream, and 255 and bit shift up to the high byte. So it takes the low byte of the 16bit source and moves it to the high byte. Leave on the internal stack.
Code:
-append
Append vertically stacks the last two items on the stack (like StackVertical). The first item is the original 16bit png, but it will be converted to 8bit by truncation of the lower 8 bits (no rounding or dithering). The next item is the lower byte shifted to the higher byte. Since we're using -depth 16, think of all numbers as 16bit. 16bit images are converted to 8bit by using the high byte, so we want to put all our results into the high byte. Finally write out the new image.

keywords: stack16, deep colour, deep color, high bit-depth, 16-bit video, timelapse, raw frames, stacked format, image magick, 16bit to stacked, stacked to 16bit, 16bit PNG

Last edited by jmac698; 10th November 2015 at 04:06.
jmac698 is offline   Reply With Quote