How do you calculate the PPI of a PDF image (XObject)?
Estimated Reading Time: 1 MinutesPPI, or Pixels per Inch, measures the resolution of bitmap images. The image itself consists of a series of pixels which are “captured” at some given resolution. Technically, an image in a PDF does not have its own DPI value, though some tools, including the Pre-Flight utility included with Adobe Acrobat, sometimes report a DPI for an image.
A PPI of a bitmap can be inferred though by parsing the image attributes. Note the values "dHorizDPI" and "dVertDPI" below:
PDEElement chkElement = PDEContentGetElem(inputPDEContent, curElement); ASInt32 elemType = PDEObjectGetType((PDEObject) chkElement); switch (elemType) { case kPDEImage: PDEImageGetAttrs ((PDEImage)chkElement, &pdeImageAttrs, sizeof(PDEImageAttrs)); // Get the dpi ASFixedMatrix matrixImage; PDEElementGetMatrix (chkElement, &matrixImage); dMatrixImgA = ( double) ASFixedToFloat(matrixImage.a); dMatrixImgD = ( double) ASFixedToFloat(matrixImage.d); dWidth72 = (pdeImageAttrs.width * 72.0f); dHeight72 = (pdeImageAttrs.height * 72.0f); dHorizDPI = (dWidth72 / dMatrixImgA); dVertDPI = (dHeight72 / dMatrixImgD); // Need to round off in case fractional DPI dpihorz = ASFixedRoundToInt32( FloatToASFixed(dHorizDPI) ); dpivert = ASFixedRoundToInt32( FloatToASFixed(dVertDPI) ); printf( "dpihorz = %d, dpivert = %d\n",dpihorz,dpivert); break;
Note that the above code does not cover all conditions. If the image is rotated using the matrix, then Matrix.a and Matrix.d are not height and width. You will need to “deRotate” the matrix to get the right answer. The Matrix does not need to be in 90 degree increments.
If the image is inside a Form, the form may have a scale factor. Depending on the scalefactor applied to the form (or forms, if it is nested), you will need to capture and pass down this collective scale factor. If the code provides a scale factor applied to the units, that also would need to be factored in.
If the image is a line drawing or scalable image, these have no inherent resolution as PDF itself has no resolution.