Photoshop has a problem with reading of certain binary PGM files

  • 2
  • Problem
  • Updated 3 years ago
  • In Progress
The binary PGM (Portable Gray Map) image file structure looks like this:

www.eecs.yorku.ca/tdb/_doc.php/man/name/pgm.5/section/5

Photoshop expects the "maximum gray value" to be 255, but in reality it can be arbitrary whole number ranging from 1 up to 255 (or 65 535 for 16-bit PGM). This number tells about how to scale the color palette. For example, when I have 2-bit PGM file, there will be 4 possible gray shades in the image. The maximum gray value in this case will be 3. In the image there can be pixels with color value of 0,1,2, or 3. When I open such image in any image viewer or editor it should scale the colors depending on the maximum value so that the pixels with highest value (of 3) will be white and some other pixels with the value of 0 will be black. IrfanView, XnView or PMView image viewers can open such files in this way.

But when you load such image into Photoshop it will not scale the values. Therefore, the image will have real colors ranging from (0,0,0) to (3,3,3). Basically, for the naked eye, the image will be just black.

This problem is only related to reading of binary PGM files. When a PGM image has ASCII encoding, the colors are scaled based on maximum gray value.

To convert any image to binary PGM file with limited bit depth, you can use for example the ImageMagick:

http://www.imagemagick.org/script/binary-releases.php

The following command will convert JPEG image to 2-bit binary PGM file:

convert anyfilename.jpg –depth 2 anyfilename.pgm
Photo of MilP

MilP

  • 58 Posts
  • 6 Reply Likes

Posted 3 years ago

  • 2
Photo of Chris Cox

Chris Cox

  • 20280 Posts
  • 813 Reply Likes
Hmm, I might have missed something in that particular code path. And because it is little used, nobody else has stumbled across this issue.

Do you have a couple sample files that we can test?
Photo of MilP

MilP

  • 58 Posts
  • 6 Reply Likes
because it is little used, nobody else has stumbled across this issue.

I like PGM and PPM files very much and use them a lot. What I like about them is simplicity. I can easily convert any data that were measured or computed into PGM ASCII files and display them as images. What I don't like about all binary and ASCII variants of PxM files is that, to my knowledge, there is no strict specification of these image formats. Therefore, it took me some time until I became acquainted with these formats.

Do you have a couple sample files that we can test?

Sure. Here you go:

pgm.zip

Please note that the ZIP archive includes the "readme.txt" file with description of images.

Good luck!
Photo of Chris Cox

Chris Cox

  • 20280 Posts
  • 813 Reply Likes
Yeah, I use them as output for command line test programs all the time, and sometimes as input. But it's definitely a programmers format.

There are specifications for PBM/PGM/PPM/PAM/PNM/PFM -- but there are also a lot of applications that are sloppy with the specs (which means I've had to make a lot of changes in the Photoshop code to allow for that sloppiness, but sometimes I make mistakes, as you've found).
Photo of Chris Cox

Chris Cox

  • 20280 Posts
  • 813 Reply Likes
OK, found the problem - we added scaling only for ASCII images and completely forgot it for binary. Looks like someone responded to a particular report and didn't think about the general case.
Photo of MilP

MilP

  • 58 Posts
  • 6 Reply Likes
That's good news. Thank you very much.