001/*
002 * $Id: ImageRenderInfo.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Kevin Day, Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf.parser;
045
046import java.io.IOException;
047
048import com.itextpdf.text.pdf.PRStream;
049import com.itextpdf.text.pdf.PdfDictionary;
050import com.itextpdf.text.pdf.PdfIndirectReference;
051import com.itextpdf.text.pdf.PdfReader;
052
053/**
054 * Represents image data from a PDF
055 * @since 5.0.1
056 */
057public class ImageRenderInfo {
058    /** The coordinate transformation matrix that was in effect when the image was rendered */
059    private final Matrix ctm;
060    /** A reference to the image XObject */
061    private final PdfIndirectReference ref;
062    /** the image object to be rendered, if it has been parsed already.  Null otherwise. */
063    private PdfImageObject imageObject = null;
064    
065    private ImageRenderInfo(Matrix ctm, PdfIndirectReference ref) {
066        this.ctm = ctm;
067        this.ref = ref;
068    }
069    
070    /**
071     * Create an ImageRenderInfo object based on an XObject (this is the most common way of including an image in PDF)
072     * @param ctm the coordinate transformation matrix at the time the image is rendered
073     * @param ref a reference to the image XObject
074     * @return the ImageRenderInfo representing the rendered XObject
075     * @since 5.0.1
076     */
077    public static ImageRenderInfo createForXObject(Matrix ctm, PdfIndirectReference ref){
078        return new ImageRenderInfo(ctm, ref);
079    }
080    
081    /**
082     * Create an ImageRenderInfo object based on inline image data.  This is nowhere near completely thought through
083     * and really just acts as a placeholder.
084     * @param ctm the coordinate transformation matrix at the time the image is rendered
085     * @param imageObject the image object representing the inline image
086     * @return the ImageRenderInfo representing the rendered embedded image
087     * @since 5.0.1
088     */
089    protected static ImageRenderInfo createdForEmbeddedImage(Matrix ctm, PdfImageObject imageObject){
090        ImageRenderInfo renderInfo = new ImageRenderInfo(ctm, null);
091        renderInfo.imageObject = imageObject;
092        return renderInfo;
093    }
094    
095    /**
096     * Gets an object containing the image dictionary and bytes.
097     * @return an object containing the image dictionary and byte[]
098     * @since 5.0.2
099     */
100    public PdfImageObject getImage() {
101        try {
102            prepareImageObject();
103            return imageObject;
104        } catch (IOException e) {
105            return null;
106        }
107    }
108    
109    private void prepareImageObject() throws IOException{
110        if (imageObject != null)
111            return;
112        
113        PRStream stream = (PRStream)PdfReader.getPdfObject(ref);
114        imageObject = new PdfImageObject(stream);        
115    }
116    
117    /**
118     * @return a vector in User space representing the start point of the xobject
119     */
120    public Vector getStartPoint(){ 
121        return new Vector(0, 0, 1).cross(ctm); 
122    }
123
124    /**
125     * @return The coordinate transformation matrix active when this image was rendered.  Coordinates are in User space.
126     * @since 5.0.3
127     */
128    public Matrix getImageCTM(){
129        return ctm;
130    }
131    
132    /**
133     * @return the size of the image, in User space units
134     * @since 5.0.3
135     */
136    public float getArea(){
137        // the image space area is 1, so we multiply that by the determinant of the CTM to get the transformed area
138        return ctm.getDeterminant();
139    }
140    
141    /**
142     * @return an indirect reference to the image
143     * @since 5.0.2
144     */
145    public PdfIndirectReference getRef() {
146        return ref;
147    }
148}