Page 1 of 1

incorrect image header in text ppm image file

Posted: 2010-06-15T14:45:24-07:00
by selden
The "magic number" of a text PPM image file created by convert is incorrectly set to P3. It should be P6

I first discovered this problem in an older version of IM running under 32bit Windows XP SP3.
I just now downloaded and installed ImageMagick-6.6.2-3-Q16-windows-dll.exe from SourceForge.
It has the same problem.

For testing, I used this command (under Cygwin)

Code: Select all

convert -size 100x100 xc:white -compress None tmp.ppm
more tmp.ppm
test.ppm starts with the lines

Code: Select all

P3
100 100
65535
65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535
The first line should be
P6

P3 is the "magic number" for a binary PPM file. P6 is the "magic number" for an ASCII PPM file.

Re: incorrect image header in text ppm image file

Posted: 2010-06-15T14:53:13-07:00
by magick
See http://en.wikipedia.org/wiki/Netpbm_format:
Each format differs in what colors it is designed to represent:
  • * PBM is for bitmaps (black and white, no grays)
    * PGM is for grayscale
    * PPM is for "pixmaps" which represent full RGB color.
Each file starts with a two-byte file descriptor (in ASCII) that explains the type of file it is (PBM, PGM, and PPM) and its encoding (ASCII or binary). The descriptor is a capital P followed by a single digit number.
  • File Descriptor Type Encoding
    P1 Portable bitmap ASCII
    P2 Portable graymap ASCII
    P3 Portable pixmap ASCII
    P4 Portable bitmap Binary
    P5 Portable graymap Binary
    P6 Portable pixmap Binary
The ASCII based formats allow for human-readability and easy transport to other platforms (so long as those platforms understand ASCII), while the binary formats are more efficient both at saving space in the file, as well as being easier to parse due to the lack of whitespace.

When using the binary formats, PBM uses 1 bit per pixel, PGM uses 8 bits per pixel, and PPM uses 24 bits per pixel: 8 for red, 8 for green, 8 for blue.

Re: incorrect image header in text ppm image file

Posted: 2010-06-15T15:03:10-07:00
by selden
Yup. Sorry. I got the two reversed :(

Re: incorrect image header in text ppm image file

Posted: 2010-06-15T18:41:48-07:00
by anthony
Easy to do. The PPM man page seems to assume you will always be dealing with binary (P6), it does not even realy mention the format details of a ASCII version of PPM. I use ASCII NetPPM images all the time as a mthod of creating images from raw data using shell scripts.

One other thing to note is that P3 (ASCII format) only does argument separation based on white space, not newlines. that is you can use spaces, tabs, returns or newlines to separate any and all arguments and data.
You can have the image on a single line, or every argument on a separate line. no problems.

the only other thing to really keep in mind is that it has a maximum resolution limit of 16 bit (65535) though ANY range maximum can also be used (it does not have to be 2^n-1 as is the case in IM). I have sometimes used this to generate images with only 5 greyscale range, or generate a image based on percentages (0 to 100). It is very versitile!

For one example. I extract morphology kernels from IM (see http://www.imagemagick.org/Usage/morphology/#showkernel ) and convert them into enlarged images for use in IM examples via a PPM in a "kernel2image" script. See http://www.imagemagick.org/Usage/morpho ... rnel2image for details.

Script is located at http://www.imagemagick.org/Usage/scripts/kernel2image