12-bit grayscale 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
jbrandon5

12-bit grayscale images

Post by jbrandon5 »

I have a series of raw format grayscale images, all with 12 bits per pixel. However, the pixels are padded with 4 bits in the MSBs, so that each pixel is 2 bytes long.

Is this the standard format for raw GRAY files? I can't seem to get IM to properly recognize these. For example, if I want to convert the image to a BMP, I've tried the following:

convert -size 4096x1714 -depth 12 gray:<input_file.raw> bmp:<output_file.bmp>

This doesn't output the correct image, and neither does using -depth 16.

What is the correct way to tell IM to handle 12-bit pixels, and do the samples need to be packed in the file, or can they be padded out to 16 bits?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: 12-bit grayscale images

Post by magick »

Add -endian lsb or -endian msb along with -depth 16 and see if that fixes the problem.
jbrandon5

Re: 12-bit grayscale images

Post by jbrandon5 »

I can see different results using the -endian option, but none of them are correct:

display -size 4096x1714 -depth 16 -endian msb gray:stem0.raw All black output
display -size 4096x1714 -depth 16 -endian lsb gray:stem0.raw Image shapes recognizable, but looks like MSBs and LSBs are swapped

display -size 4096x1714 -depth 12 -endian msb gray:stem0.raw Image shapes recognizable, but strong vertical striations
display -size 4096x1714 -depth 12 -endian lsb gray:stem0.raw Same as previous one


The pixels are stored big-endian, so I would expect that option to work, but something is still off. Any other ideas? Thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: 12-bit grayscale images

Post by magick »

We have a couple of ideas but to save time can you post a URL to one of your images and its width and height. We'll get it displayed properly and then post the proper command here.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: 12-bit grayscale images

Post by glennrp »

You should have padded them in the lsb. Your "black" images may be really very dark gray,
with values 0-3855. To view the existing images you will have to multiply each pixel by 17.
jbrandon5

Re: 12-bit grayscale images

Post by jbrandon5 »

Yes, that was the problem. These images were output from a software JPEG2000 decoder, and there was no option where to place the 12-bit samples inside the 16 bits (the MSBs were padded), so I had to rewrite the files myself in order to view them.

Then this was the correct command: display -size 4096x1714 -depth 16 -endian msb gray:input.raw gray:output.raw

It appears that the -depth option tells IM to always expect packed samples for a RAW pixel format. It would be useful if the documentation stated this, since I originally was confused whether to use -depth 12 or -depth 16.

Thanks for your assistance!
Post Reply