PNG-spec icon indicating copy to clipboard operation
PNG-spec copied to clipboard

pHYs is metric-only

Open svgeesus opened this issue 4 years ago • 4 comments

Raised by: Dirk Schenkewitz Originally raised: 21 Oct 2005

Dear All,

several years ago when I read the PNG specification for the first time, I saw a little design flaw, regarding the pHYs-tag. The only "known unit" is metre and the resolution value must be a 4-bit integer.

Now we all know that practically all existing devices have their resolution defined in "dpi" (dots per inch), while an inch is 25.4mm.

That means that it is impossible to specify the resolution value of a real-world device without losing some precision, because 1m obviously cannot be divided by 25.4mm without a remainder.

One possible solution: Specify that unit = 2 means the unit is "inch".

I'm a bit astonished that over the years nobody seemed to notice this, at least I did not find any hint in the archives for "dpi" or "inch" and only one for "metre" which is about spelling.

Is it possible that nobody ever uses this tag?

Best regards Dirk Schenkewitz

svgeesus avatar Jan 19 '21 14:01 svgeesus

Looking at 11.3.5.3 pHYs Physical pixel dimensions I suspect this comment has misunderstood the use of pHYs. It is not primarily used to specify resolution (such as "300 dpi") but rather to indicate the aspect ratio of non-square pixels.

If the pHYs chunk is not present, pixels are assumed to be square, and the physical size of each pixel is unspecified.

Also, the resolution in x and y is a four byte integer, not a four bit integer.

My feeling is that this chunk is not used very much, and when it is, it is to capture non-square pixels (for example, to archive images from some other format). However, the inability to accurately capture a physical resolution in Imperial units is well put, and possibly should be corrected or another chunk introduced to do this, if there is a need.

svgeesus avatar Jan 19 '21 15:01 svgeesus

I believe you are correct that it is perhaps rarely used. And when it is, it is probably mostly used to specify non-square pixels. But that linked spec does say:

The pHYs chunk specifies the intended pixel size or aspect ratio for display of the image.

(emphasis my own)

If unit is 1, it would specify a physical resolution. And since DPI is such a common way to specify resolution, it makes sense to me to add an inch unit. That said, US units are now all defined in terms of SI units. So I could also be convinced that adding inches isn't beneficial.

I think I am leaning toward adding an inch unit. What are your thoughts?

ProgramMax avatar Sep 16 '21 21:09 ProgramMax

We probably should (and add required handling for unknown unit specifiers).

In addition that section really needs a worked example, like: 72dpi

72 ÷ 25.4 × 1,000 = 2,834.64566929 = 2835 2,835 ÷ 1,000 × 25.4 = 72.009

Before doing so it would be good to run some tests to see what image display systems do with non-square pixels or with physical dimensions. PngSuite has a few tests:

For the physical pixel dimensions, the result of each decoding should be a square picture. The first (cdf) image is an example of flat (horizontal) pixels, where the pHYS chunk (x is 1 per unit, y = 4 per unit) must take care of the correction. The second is just the other way round. The last example uses the unit specifier, for 1000 pixels per meter. This should result in a picture of 3.2 cm square.

cdf - physical pixel dimensions, 8x32 flat pixels cdh - physical pixel dimensions, 32x8 high pixels cds - physical pixel dimensions, 8x8 square pixels cdu - physical pixel dimensions, with unit-specifier

results show browsers ignore non-square pixels and ignore physical dimensions. Need to check against CSS and HTML specs to see correct handling there.

svgeesus avatar Jul 17 '23 16:07 svgeesus

Related:

  • https://github.com/w3c/PNG-spec/issues/97

svgeesus avatar Jul 17 '23 16:07 svgeesus