Convert gray PPM to PGM, PNG

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
wuzi
Posts: 1
Joined: 2012-11-01T09:01:38-07:00
Authentication code: 67789

Convert gray PPM to PGM, PNG

Post by wuzi »

ImageMagick 6.7.7-10 on Ubuntu 12.10 x86_64.

There is a gray PPM image 8x1 "00 20 40 60 80 a0 c0 e0"

Code: Select all

$ cat test.ppm 
P3
8 1
255
0
0
0
32
32
32
64
64
64
96
96
96
128
128
128
160
160
160
192
192
192
224
224
224
ppmtopgm converts correctly from PPM to PGM: "00 20 40 60 80 a0 c0 e0"

Code: Select all

$ ppmtopgm test.ppm | hexdump -C
00000000  50 35 0a 38 20 31 0a 32  35 35 0a 00 20 40 60 80  |P5.8 1.255.. @`.|
00000010  a0 c0 e0                                          |...|
00000013 
When converting this PPM image using ImageMagick the result is wrong: "00 04 0d 1e 37 5a 86 be"

Code: Select all

$ convert test.ppm pgm: | hexdump -C
00000000  50 35 0a 38 20 31 0a 32  35 35 0a 00 04 0d 1e 37  |P5.8 1.255.....7|
00000010  5a 86 be                                          |Z..|
00000013
The same problem exists with the convertion from PPM to PNG and back: "00 04 0d 1e 37 5a 86 be"

Code: Select all

$ convert test.ppm png: | convert - ppm: | hexdump -C
00000000  50 36 0a 38 20 31 0a 32  35 35 0a 00 00 00 04 04  |P6.8 1.255......|
00000010  04 0d 0d 0d 1e 1e 1e 37  37 37 5a 5a 5a 86 86 86  |.......777ZZZ...|
00000020  be be be                                          |...|
00000023
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert gray PPM to PGM, PNG

Post by snibgo »

It seems to work okay for me. (Windows, 6.7.9-Q16)

Code: Select all

D:\web\im>"%IMG%convert" test.ppm test.png

D:\web\im>"%IMG%convert"  test.png text:-
# ImageMagick pixel enumeration: 8,1,255,srgb
0,0: (  0,  0,  0)  #000000  black
1,0: ( 32, 32, 32)  #202020  srgb(32,32,32)
2,0: ( 64, 64, 64)  #404040  grey25
3,0: ( 96, 96, 96)  #606060  srgb(96,96,96)
4,0: (128,128,128)  #808080  fractal
5,0: (160,160,160)  #A0A0A0  srgb(160,160,160)
6,0: (192,192,192)  #C0C0C0  silver
7,0: (224,224,224)  #E0E0E0  grey88

D:\web\im>"%IMG%convert"  test.png ppm:test2.ppm

D:\web\im>dumpbin /itest2.ppm /o- /a-
      Input: /i test2.ppm
test2.ppm
{50}{36}\n
{38}{20}{31}\n
{32}{35}{35}\n
{00}{00}{00}{20}{20}{20}{40}{40}{40}{60}{60}{60}{80}{80}{80}{a0}{a0}{a0}{c0}{c0}
{c0}{e0}{e0}{e0}
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert gray PPM to PGM, PNG

Post by fmw42 »

This is what I get on IM 6.8.0.3 Q16 Mac OSX Snow Leopard

convert test.ppm txt:
# ImageMagick pixel enumeration: 8,1,255,srgb
0,0: ( 0, 0, 0) #000000 black
1,0: ( 32, 32, 32) #202020 srgb(32,32,32)
2,0: ( 64, 64, 64) #404040 grey25
3,0: ( 96, 96, 96) #606060 srgb(96,96,96)
4,0: (128,128,128) #808080 fractal
5,0: (160,160,160) #A0A0A0 srgb(160,160,160)
6,0: (192,192,192) #C0C0C0 silver
7,0: (224,224,224) #E0E0E0 grey88

PPM I believe is linear for grayscale and PNG will be non-linear sRGB. So I think it is a colorspace issue, since after IM 6.7.8.3 grayscale was treated as linear see viewtopic.php?f=4&t=21269 and http://www.imagemagick.org/script/forma ... colorspace

This gives the wrong results due to the linear to sRGB conversion:
convert test.ppm -set colorspace RGB test.png
convert test.png txt:
# ImageMagick pixel enumeration: 8,1,255,srgb
0,0: ( 0, 0, 0) #000000 black
1,0: ( 4, 4, 4) #040404 srgb(4,4,4)
2,0: ( 13, 13, 13) #0D0D0D grey5
3,0: ( 30, 30, 30) #1E1E1E srgb(30,30,30)
4,0: ( 55, 55, 55) #373737 srgb(55,55,55)
5,0: ( 90, 90, 90) #5A5A5A srgb(90,90,90)
6,0: (134,134,134) #868686 srgb(134,134,134)
7,0: (190,190,190) #BEBEBE grey

But by forcing the conversion to to PNG to think it is already linear, then it won't make the PNG sRGB. Do that by adding -set colorspace RGB.

This gives the same result as the PPM
convert test.ppm -set colorspace RGB test.png
convert test.png txt:
# ImageMagick pixel enumeration: 8,1,255,gray
0,0: ( 0, 0, 0) #000000 gray(0,0,0)
1,0: ( 32, 32, 32) #202020 gray(32,32,32)
2,0: ( 64, 64, 64) #404040 gray(64,64,64)
3,0: ( 96, 96, 96) #606060 gray(96,96,96)
4,0: (128,128,128) #808080 gray(128,128,128)
5,0: (160,160,160) #A0A0A0 gray(160,160,160)
6,0: (192,192,192) #C0C0C0 gray(192,192,192)
7,0: (224,224,224) #E0E0E0 gray(224,224,224)


As to whether this is a bug or not, the IM developers would need to respond.
Post Reply