# How to determine if a PDF Image is transformed and by how much

Q:

So, if I have an image that is rotated, scaled, skewed, how could I get the matrix decomposed?

If my matrix contains all the possible transformation, have I to decompose the matrix (obtaining some intermediate matrix subtracting something) or could I find my values (rotating degrees, scaling factor, etc….) in another manner?

## Below there is written “if the image is rotated” and then I find the scaling value. What could I do with it?

A:

First of all, if you just need to know the resolution of an image, which is independent of the transformation imposed on it, you can simply use Element::GetImageWidth() and Element::GetImageHeight(). These two functions give you the number of pixels of the image in the canonical space.

When rasterized, an image is first mapped to page space (by CTM that is determined by the PDF content, which you can get by calling Element::GetCTM()) and then mapped to client space (by a user matrix U, e.g., determined by the zoom value). So, you can be talking about CTM, or U*CTM. Either way, given an affine transformation matrix M, it can be decomposed as M = t * r * sc * sh, where t is a translation matrix, r is a rotation matrix, sc is a scaling matrix and sh is a shear matrix. Note that this decomposition is not necessarily unique. You can find lots of information online (e.g., http://callumhay.blogspot.ca/2010/10/decomposing-affine-transforms.html) We will expose the decomposition method in Matrix2D class we use internally in the next release.

thank you for the explanation, it’s been very useful for me.

Only a question.

We usually work with PDF files without visualizing them in a pdfviewer. So I think the “visual factor” could be excluded.

Given a Matrix2D on an image, could I understand if it contains a translation, a rotation, a scaling or a shear matrix only looking at it?

Because if I want the image as its original state (before being included in a PDF…) I have to understand its data.

So I should understand if looking at a given matrix I can find out if the image is rotated, is scaled, only looking at it. Is it correct?
If the image is only translated, it’s simple, also if it’s only rotated. But if the image is either rotated, scaled, translated, could I understand it in order to extract the image and compare it with the one included in the pdf?

Suppose the original image embedded in the PDF file is S and S is subject to some transform that maps it into PDF page space. This transform is decided soly by the content stream in the PDF file. Denote the transformed image in the page space as T. So you want to know how T is transformed, right?

## Denote T = M(S), where M is the matrix that transforms S to T. If you can decompose M = t * r * sc * sh as mentioned in the previous email, you will be able to tell if it is translated, rotated, scaled, or sheared and by how much. Now the problem you have to address is to compute M using PDFNet APIs. Roughly speaking, you can compute M = CTM * P, where CTM is the current transformation matrix and P is the image transformation matrix. The following is the code snippet:

Element* image_element;
… //get the image element

Matrix2D CTM = image_element->GetCTM(); //get the CTM

int width = image_element->GetImageWidth();
int height = image_element->GetImageHeight();

Matrix2D P(1.0/width, 0, 0, -1.0/height, 0, 1); //get P. This is because PDF spec. says S is first mapped to a unit square upside down, and then subject to CTM

## Matrix2D M = CTM * P; //here is your M that can be decomposed

On Thursday, October 18, 2012 12:00:35 PM UTC-7, Frank Liu wrote:

thank you for the explanation, it’s been very useful for me.

Only a question.

We usually work with PDF files without visualizing them in a pdfviewer. So I think the “visual factor” could be excluded.

Given a Matrix2D on an image, could I understand if it contains a translation, a rotation, a scaling or a shear matrix only looking at it?

Because if I want the image as its original state (before being included in a PDF…) I have to understand its data.

So I should understand if looking at a given matrix I can find out if the image is rotated, is scaled, only looking at it. Is it correct?
If the image is only translated, it’s simple, also if it’s only rotated. But if the image is either rotated, scaled, translated, could I understand it in order to extract the image and compare it with the one included in the pdf?