Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
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
Old 10th August 2016, 21:27   #2  |  Link
jmac698
Registered User
 
Join Date: Jan 2006
Posts: 1,867
Updated for Image Magick 7

Code:
magick IMG_2005.pgm -depth 16 ( +clone -evaluate and 255 -evaluate multiply 256 ) -append stacked.png
16bit tiff, pgm, png files can be used as input also.
jmac698 is offline   Reply With Quote
Old 6th April 2017, 11:06   #3  |  Link
yup
Registered User
 
Join Date: Feb 2003
Location: Russia, Moscow
Posts: 854
load stacked images in Avisynth

jmac698!

How load this stacked images in Avisynth?
You try it?

yup.
yup is offline   Reply With Quote
Old 7th April 2017, 19:33   #4  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 407
It should work something like this - don't take it literally, please.
I made it in early february to process DNG or RAW files with AviSynth.
'Me' is just an object oriented programming mimicking shortcut of the script base name, the ImageMagick commands should work when their path is in the %PATH% variable and you need the CallCmd plugin.

Code:
function Me() {
	Try { LeftStr(ScriptName(), StrLen(ScriptName())-4) }
		catch (err_msg) { assert(false, "'ScriptName()' is available from v2.60 only") }
}

if (!exist(Me+".png")) {
    cmd = "cmd /c convert "+Me+" -depth 16 ( +clone -evaluate and 255 -evaluate multiply 256 ) -append -depth 8 "+Me+".png"
    ColorBars.CallCmd(Hide=true,Debug=false,Open=cmd,Synchronous=7)
}
ImageReader(Me+".png")

#########################################
# add your stacked format processing here
#########################################

ImageWriter(Me+".png", 0, 0, type="png")

cmd = "cmd /c convert "+Me+"000000.png -crop 100%x50% ( +clone -evaluate divide 256 ) -delete 1 -compose Add -composite -define png:bit-depth=16 "+Me+".out.png"
CallCmd(Hide=true,Debug=false,Close=cmd,Synchronous=7)
EDIT: I notice that as shown, the script requires AviSynth+ to understand the if (...) {...} part. With classic AviSynth, please use the GScript plugin and put GScript(""" ... """) around it.

Last edited by martin53; 9th April 2017 at 10:24.
martin53 is offline   Reply With Quote
Old 10th April 2017, 18:24   #5  |  Link
yup
Registered User
 
Join Date: Feb 2003
Location: Russia, Moscow
Posts: 854
Hi martin53!

pinterf help me solve this problem:
https://forum.doom9.org/showthread.p...52#post1802952
yup.
yup is offline   Reply With Quote
Old 10th April 2017, 18:32   #6  |  Link
pinterf
Registered User
 
Join Date: Jan 2014
Posts: 2,314
Anyway, in the next avs+ version you can import it directly as RGB48 or RGB64 without any trick.
pinterf is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:23.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.