How to convert 16bit signed, little endian RAW to PNG

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?".
Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

OOooo! Finally!! This is the one I wanted!
snibgo and magick, thank you soo much!
I spent 2 days to resolve this issue. Cannot thank you enough!

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

magick wrote:
2020-03-04T07:48:18-07:00
The quantum:format=signed define has been around several years. The results we obtained from the proposed command matched the expected results. Not sure why its failing for you. @snibgo's command works as well except its AddModulus (no space).
It is not listed as a quantum-format option on the defines section at https://imagemagick.org/script/command- ... php#define. Would you add all the options available for -define quantum-format on the page.

However, I, now, do see:

Code: Select all

convert -list quantum-format
FloatingPoint
Signed
Unsigned

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

Try this using -define quantum:format=signed

Code: Select all

convert -size 512x512 -depth 16 -define quantum:format=signed gray:original.raw -auto-level result.png

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

"-define quantum:format=signed" is not working for me somehow.
I'm usign ImageMagick 6.7.8-9 btw.
The image won't be correct unless I add "-evaluate AddModulus 50%".

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

I do not know when that was introduce. But your version 6.7.8.9 is ancient and know to have some bugs. I would suggest that you upgrade

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

Another problem:(
I showed the png image to my client. He said that the display range is wrong.

Display range of original.raw and firstimage.raw is "-2048 - 1623". I can see it in image info on ImageJ.
However, the display range of png from IM is "0 - 65535".

If I convert original.raw to png with ImageJ, generated png becomes "30720 - 34391"
Is there any way to make png same as the one fom ImageJ?

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

This does what you ask, but I do not think it matches your ImageJ result

Code: Select all

convert -size 512x512 -depth 16 -define quantum:format=signed gray:original.raw -auto-level +level 30720x34391 result.png
identify -verbose result.png
Gray:
min: 30720 (0.468757)
max: 34391 (0.524773)

I do not know what ImageJ is doing with the negative values.

If I take your firstimage.raw and open it in ImageJ and then save to PNG, the resulting values are:

min: 0 (0)
max: 65535 (1)

So I do not know where you are getting 30720 - 34391

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

Thanks a lot fmw42!
It is now "Display range: 30720 - 34391" on ImageJ and I think that is what my client wants.
The range is same as the one generated from ImageJ.

Hope my client is happy with the result this time!
Last edited by Ash7890 on 2020-03-04T22:34:21-07:00, edited 2 times in total.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

But your PNG does not have that range. It is 0 to 65535 as I pointed out above. Save the PNG file in ImageJ and then open it from the saved file and see what it says the values are. That is what is the important values. Not what ImageJ says it is from the original.

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

I opened firstimage.raw in ImageJ and saved as PNG. Then opened PNG in ImageJ.
It says "Display range: 30720 - 34391". Looks OK to me.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to convert 16bit signed, little endian RAW to PNG

Post by fmw42 »

Ash7890 wrote:
2020-03-04T22:40:30-07:00
I opened firstimage.raw in ImageJ and saved as PNG. Then opened PNG in ImageJ.
It says "Display range: 30720 - 34391". Looks OK to me.
That is not what I get when closing the image that was opened and change to PNG and then opening the PNG file that was save to disk.

Nevertheless, I think I have figured it out. In addition to setting the quantum-format to signed, you need to set the endian to MSB. That is the final key and it works for me with this command.

Code: Select all

magick -size 512x512 -depth 16 -define quantum:format=signed -endian MSB gray:original.raw result.png
This result looks like the result that I get when I save your raw file to PNG in ImageJ.

Input Raw file:
http://www.fmwconcepts.com/misc_tests/r ... timage.raw

My result from ImageMagick:
Image

My result from opening raw file in ImageJ and saving it as PNG:
Image

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

Not sure if this question should be posted to a new thread...

I now want to combine "head" command and "convert" command, so I do not need to create a intermediate file. I researched a bit and found miff and mpr. However, as head is not IM command, it is not the case unfortunately.

It would take ages if I run below 1000+ to process each image:

Code: Select all

head --bytes 524288 original.raw | tail --bytes 524288 > single_img.raw (bytes will be changed for second, third.. images)
convert -size 512x512 -depth 16 gray:single_img.raw -evaluate AddModulus 50% -auto-level +level 30720x34391 result.png
Is there a way something like this instead?:

Code: Select all

head --bytes 524288 original.raw | tail --bytes 524288 > convert -size 512x512 -depth 16 gray:(result from head) -evaluate AddModulus 50% -auto-level +level 30720x34391 result.png

Ash7890
Posts: 18
Joined: 2020-03-02T23:53:35-07:00
Authentication code: 1152

Re: How to convert 16bit signed, little endian RAW to PNG

Post by Ash7890 »

I found the answer:

Code: Select all

head --bytes 524288 original.raw | convert -size 512x512 -depth 16 gray:- -evaluate AddModulus 50% -auto-level +level 30720x34391 result.png

Locked