The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.
There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.
See this Wikipedia section for the details: Converting color to grayscale
I believe you want to use the formula for Csrgb
.
Try it out and see if it matches the results you're expecting.
Basically, you'll do this:
- Take
R, G, B
color (each in[0,1]
range)- If they're in the range
0..255
instead, simply divide by255.0
- If they're in the range
- Compute
Clinear = 0.2126 R + 0.7152 G + 0.0722 B
- This is likely the linear transform you were applying before
- Compute
Csrgb
according to it's formula, based onClinear
- This is the nonlinear gamma correction piece you were missing
- Check out this WolframAlpha plot
Csrgb = 12.92 Clinear
whenClinear <= 0.0031308
Csrgb = 1.055 Clinear1/2.4 - 0.055
whenClinear > 0.0031308