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

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

Post by Ash7890 »

I'm new to Imagemagick and straggling to convert RAW to PNG.

My client gave me a RAW data which contains multiple images. The size is 512*512, 16bit signed and little endian.
So, I cropped one of those with:

Code: Select all

convert -size 512x512 -depth 16 UYVY:original.raw result.raw
This is the corpped RAW:
https://drive.google.com/file/d/1V1c-yt ... sp=sharing

It looks like this on ImageJ (with 16bit signed, little endian option):

https://drive.google.com/file/d/1ziTATE ... i7Xft/view

I want a PNG looks same as above image. However, I have no idea the options for signed and little endian. One of many commands I tried is like:

Code: Select all

convert -size 512x512 -depth 16 -define png:swap-bytes=on -define signed uyvy:result.raw result.png
It generates weird greenish image. My client told me the RAW is 1ch(mono), so should be converted to 3ch(rgb) gray and then convert to PNG.
Last edited by Ash7890 on 2020-03-03T17:55:12-07:00, edited 1 time in total.

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

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

Post by snibgo »

Please paste a link to original.raw.
snibgo's IM pages: im.snibgo.com

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 »

snibgo wrote:
2020-03-03T09:58:20-07:00
Please paste a link to original.raw.
Oops, forgot to provide it! I pasted it on the original post under "This is the corpped RAW:"

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 »

Why do you say it is cropped? I see no cropping in your command. All it seem, to try to do is convert from raw to 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 »

fmw42 wrote:
2020-03-03T17:59:02-07:00
Why do you say it is cropped? I see no cropping in your command. All it seem, to try to do is convert from raw to png.
Probably because of my luck of knowledge of raw data. As I explained above, the original raw contains more than 1000 of 512*512 raw image data. The link to raw I provided above is one of those. I wanted to get just one of them for testing. Im calling it "cropped". Sorry for your confusion.

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 »

No problem. Just wanted to be sure you had not attempted to process the raw data in any way that might have corrupted it.

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 think it is not corrupted as it can be opened with ImageJ without any error and it looks correct.

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

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

Post by snibgo »

To get just one image, you read your client's file, original.raw with prefix UYVY: and saved the result as result.raw with no prefix. When I try that, I get an error: "no encode delegate for this image format `RAW'". What format do you think result.raw is?

I am confused "My client told me the RAW is 1ch(mono)". If there is only one channel, it is "gray:" not "uyvy:".
Ash7890 wrote:-define png:swap-bytes=on
I suggest "-endian" instead. See https://www.imagemagick.org/script/comm ... php#endian
snibgo's IM pages: im.snibgo.com

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 am confused "My client told me the RAW is 1ch(mono)". If there is only one channel, it is "gray:" not "uyvy:".
I thought the same, and tried prefix "gray", however I got an error:

Code: Select all

convert -size 512x512 -depth 16 gray:original.raw result.raw
convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
Image info of the original.raw on ImageJ is below. It is saying Unit: "Gray Value".
Title: original.raw
Width: 512 pixels
Height: 512 pixels
Depth: 10 pixels
Size: 5MB
Voxel size: 1x1x1 pixel^3
ID: -4
Bits per pixel: 16 (signed)
Display range: -2048 - 1940
Image: 1/1000
No threshold
ScaleToFit: false

Calibration function: y = a+bx
a: -32768.000000
b: 1.000000
Unit: "Gray Value"
What format do you think result.raw is?
I thought it was the same as the original.raw.
result.raw on ImageJ is saying Unit: "Gray Value" too.

I also tried -endian. PNG was generated but it was the wrong color.

Code: Select all

convert -size 512x512 -depth 16 -endian MSB -define signed gray:result.raw result.png

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

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

Post by snibgo »

Ash7890 wrote: convert -size 512x512 -depth 16 gray:original.raw result.raw
convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
Your original.raw is very large, isn't it? Perhaps IM tries to read it all into memory.

Your convert command will change the storage format, so I think this is a bad way of getting just the first of 1000 images. If you are sure each pixel takes 2 bytes, you want to make a new file from the first 512*512*2 bytes, 524288 bytes. The unix tool "head" can do that:

Code: Select all

head --bytes 524288 original.raw >firstimage.raw
When you have done that, please paste a link to firstimage.raw

(When you want the second or third image, you can use "head" then "tail".)
snibgo's IM pages: im.snibgo.com

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 »

Yes, original.raw is large. Here is firstimage.raw generated from "head":

https://drive.google.com/file/d/1CmKPdw ... sp=sharing

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

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

Post by magick »

Try this command:

Code: Select all

convert -size 512x512 -depth 16 -endian LSB -define quantum:format=signed gray:result.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 »

Mmm it still looks different. I tried both result.raw and firstimage.raw (which is made from "head" command). Both look the same.

https://drive.google.com/file/d/1YaGmII ... sp=sharing

If you actually tried that command and it worked, then the problem might be my environment??

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

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

Post by snibgo »

Okay, good. That file firtimage.raw is identical to the original.raw you uploaded previously. Nevertheless, I suggest you use "head" and "tail" for future image extraction.

I think "-define quantum:format=signed" is new to IM, possibly newer than your version.

To get the same result as you got from ImageJ:

Code: Select all

magick -size 512x512 -depth 16 gray:firstimage.raw -evaluate AddModulus 50% -auto-level x.png
If using Windows BAT scripts, double the % to %%.

EDIT: Corrected typo: no space in "AddModulus".
snibgo's IM pages: im.snibgo.com

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

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

Post by magick »

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).

Locked