jpeg pixel aspect ratio

hi everyone,
i am building a kpeg parser and i have recently written code to parse the app0 segment JFIF ff e0. Everything is fine but i have noticed that i have a ton of phoos which contain strange value for the xdensity and ydensity fields. Whenever the unit is set to 0, the densities are supposed to be pixel aspect ratio, so far as i have seen in text about the unit field. Anyway, a field value of 1 makes sense to me: 1:1 par. However, i see a field value of 100 for alot of photos. how can this be a par value? 100:100? are we supposed to divide the field values or read them as is? as is yields 100:100 which i don’t understand how it is a valid pixel aspect ratio. Anyone able to help understand what to do with this field? 100/100 is 1. In addition to this info, Microsoft Windows changes these values to dpi and the dpi varies (no exif data present either, so where are the values coming from?)

windows example 1: unit 0, xdensity 100 (0x64), ydensity 100 (0x64). Microsoft reports 508 dpi. okay, 100+100=200 * 2.54 = 508 but there is no field in the app0 segment that specifies add these numbers. furthermore, the same values in other photos are reported by Microsoft as 96 dpi, a calculation that i cannot achieve via 0x64.

i assume that these fields should be divided but nothing is mentioned in the specification. any ideas?

This is how I understand the specification.

First you need the value of the “unit” field:

0: no unit, just pixel ratio
1: dpi
2: dots per cm

When the unit is 0, you have just to divide the two values to get the aspect ratio. No density given. Otherwise, you get an X and Y density (e.g. for print) and can also derive the aspect ratio from that.

3 Likes

Hi Thomas,

i thank you for the lovely reply. You are very kind to take time from your day to offer a response. I appreciate it very much. earnestly.

so do you suggest that i code to check if value is greater than 1 then divide xdensity by ydensity and leave it at that? or should i just remove the suffix and show only the value in the file?

when i say suffix, i mean that i read the unit and report what it is and explain it to the user. like so: unit 0 = pixel aspect ratio (par)

then i extract the xdensity and ydensity values and display them like so:
xdensity 100 par, ydensity 100 par

100 seems wrong, which is why i posted here. i will remove the suffix (par) and either display 100 or 100/100.

i also have images which should be dots per centimeter but the value in the jpeg is actually dpi (72, 200 etc). so i am stuck at wondering if i should multiply by 0.393701 o get the actual dpcm or also leave it as 72, 200 etc.

i hesitate to make a decision because i do not know what someone may purposely place there as a value. i am just wondering if you or anyone else has a suggestion for my code? leave the values alone or perform a calculation? i just do not want users of my program to complain that the values are not correct.

Can you post the actual values of some typical JPG-files: unit, x-density, density?

That would help.

100 seems wrong, which is why i posted here.

The problem is that some software, when it reads the unit field “0: no unit, just pixel ratio”, assumes the units are actually dots per inch. So if the value is “1”, this is read as “1 dot per inch”, which can cause problems when printing the image, or compositing images, and so on.

So when software writes the jpeg, when there are no units (or units are unknown), it may be safer to write “100” rather than “1”.

2 Likes

In that example the aspect ratio is 100/100=1. No density given. Is that correct for the image in question?

1 Like

okay, so i used a php script to read the app0 markers of four images used in this discussion. The output from this script comes first, then my programs output code follows.

two images using unit = 0. The first image has 0x64 values (100) and the second contains normal par values 1:1

00 | | 5
10 | | 6
4a | J | 7
46 | F | 8
49 | I | 9
46 | F | 10
00 | | 11
01 | | 12
02 | | 13
00 | | 14 unit
00 | | 15 xdensity
64 | d | 16 xdensity
00 | | 17 ydensity
64 | d | 18 ydensity
00 | | 19
00 | | 20

JFIF-Version	0102 (1.02)
Unit	0 = Pixel Aspect Ratio (par)
X Density	100
Y Density	100
X Thumbnail	0
Y Thumbnail	0


00 | | 5
10 | | 6
4a | J | 7
46 | F | 8
49 | I | 9
46 | F | 10
00 | | 11
01 | | 12
01 | | 13
00 | | 14 unit
00 | | 15 xdensity
01 | | 16 xdensity
00 | | 17 ydensity
01 | | 18 ydensity
00 | | 19
00 | | 20

JFIF-Version	0101 (1.01)
Unit	0 = Pixel Aspect Ratio (par)
X Density	1
Y Density	1
X Thumbnail	0
Y Thumbnail	0

the next two images contain unit = 2 or dots per centimeter but they contain hex values which are dots per inch values. (i think that i understand here that one byte is not enough to store a float value or numerator/denominator values. i guess that i should just multiply the values by 0.393701.)

00 | | 5
10 | | 6
4a | J | 7
46 | F | 8
49 | I | 9
46 | F | 10
00 | | 11
01 | | 12
01 | | 13
02 | | 14
00 | | 15
48 | H | 16
00 | | 17
48 | H | 18
00 | | 19
00 | | 20

JFIF-Version	0101 (1.01)
Unit	2 = Dots per centimeter (dpcm)
X Density	72
Y Density	72
X Thumbnail	0
Y Thumbnail	0


00 | | 5
10 | | 6
4a | J | 7
46 | F | 8
49 | I | 9
46 | F | 10
00 | | 11
01 | | 12
02 | | 13
02 | | 14
00 | | 15
c8 | | 16
00 | | 17
c8 | | 18
00 | | 19
00 | | 20

JFIF-Version	0102 (1.02)
Unit	2 = Dots per centimeter (dpcm)
X Density	200
Y Density	200
X Thumbnail	0
Y Thumbnail	0

so whenever i noticed the values in the par images, i decided to post here for tips. 100 par looks silly in my output. I assume that i am supposed to divide the two numbers but i am not sure about it now. I was thinking to leave the value encoded in the file, here 100 and use no suffix after that value since i describe the unit in the output.

thoughts? recommendations? is my included code helpful to you?

The “pixel aspect ratio” is not a single number. It is a ratio between two numbers, the width and height of each pixel. So “1:1” or “100:100” would both mean the pixels are square.

But the ratio can always be expressed as “1:N” where N is a number that you calculate by dividing the Y-resolution by the X-resolution. Those two resolutions are often the same, because images often have square pixels.

The only images with non-square pixels I have encountered are fax images. But see also Pixel aspect ratio - Wikipedia .

1 Like

Hi snibgo,

you have been very helpful in this topic and i appreciate you very much. I see what you mean and so xdensity / ydensity is the answer. I just have to add the code and i am finished with app0. I am kind of working backward with parsing as i have tackled more complex segments such as exif and icc profile headers. I have allready extracted frame headers and added 100% validation. I figure it is time to deal with app0 and also to validate da headers against the frame header. I did not expect a value of 100 (0x64) so i thought that it will be better if i ask some jpeg professionals. I appreciate all of the help. :slight_smile: