001/*
002 * $Id: ICC_Profile.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: 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;
045
046import java.io.FileInputStream;
047import java.io.InputStream;
048import java.util.HashMap;
049
050import com.itextpdf.text.ExceptionConverter;
051import com.itextpdf.text.error_messages.MessageLocalization;
052
053public class ICC_Profile {
054    protected byte[] data;
055    protected int numComponents;
056    private static HashMap<String, Integer> cstags = new HashMap<String, Integer>();
057
058    protected ICC_Profile() {
059    }
060
061    public static ICC_Profile getInstance(byte[] data) {
062        try {
063            if (data.length < 128 || data[36] != 0x61 || data[37] != 0x63
064                || data[38] != 0x73 || data[39] != 0x70)
065                throw new IllegalArgumentException(MessageLocalization.getComposedMessage("invalid.icc.profile"));
066            ICC_Profile icc = new ICC_Profile();
067            icc.data = data;
068            Integer cs = cstags.get(new String(data, 16, 4, "US-ASCII"));
069            icc.numComponents = cs == null ? 0 : cs.intValue();
070            return icc;
071        }
072        catch (Exception ex) {
073            throw new ExceptionConverter(ex);
074        }
075    }
076
077    public static ICC_Profile getInstance(InputStream file) {
078        try {
079            byte[] head = new byte[128];
080            int remain = head.length;
081            int ptr = 0;
082            while (remain > 0) {
083                int n = file.read(head, ptr, remain);
084                if (n < 0)
085                    throw new IllegalArgumentException(MessageLocalization.getComposedMessage("invalid.icc.profile"));
086                remain -= n;
087                ptr += n;
088            }
089            if (head[36] != 0x61 || head[37] != 0x63
090                || head[38] != 0x73 || head[39] != 0x70)
091                throw new IllegalArgumentException(MessageLocalization.getComposedMessage("invalid.icc.profile"));
092            remain = (head[0] & 0xff) << 24 | (head[1] & 0xff) << 16
093                      | (head[2] & 0xff) <<  8 | head[3] & 0xff;
094            byte[] icc = new byte[remain];
095            System.arraycopy(head, 0, icc, 0, head.length);
096            remain -= head.length;
097            ptr = head.length;
098            while (remain > 0) {
099                int n = file.read(icc, ptr, remain);
100                if (n < 0)
101                    throw new IllegalArgumentException(MessageLocalization.getComposedMessage("invalid.icc.profile"));
102                remain -= n;
103                ptr += n;
104            }
105            return getInstance(icc);
106        }
107        catch (Exception ex) {
108            throw new ExceptionConverter(ex);
109        }
110    }
111
112    public static ICC_Profile GetInstance(String fname) {
113        FileInputStream fs = null;
114        try {
115            fs = new FileInputStream(fname);
116            ICC_Profile icc = getInstance(fs);
117            return icc;
118        }
119        catch (Exception ex) {
120            throw new ExceptionConverter(ex);
121        }
122        finally {
123            try{fs.close();}catch(Exception x){}
124        }
125    }
126
127    public byte[] getData() {
128        return data;
129    }
130
131    public int getNumComponents() {
132        return numComponents;
133    }
134
135    static {
136        cstags.put("XYZ ", Integer.valueOf(3));
137        cstags.put("Lab ", Integer.valueOf(3));
138        cstags.put("Luv ", Integer.valueOf(3));
139        cstags.put("YCbr", Integer.valueOf(3));
140        cstags.put("Yxy ", Integer.valueOf(3));
141        cstags.put("RGB ", Integer.valueOf(3));
142        cstags.put("GRAY", Integer.valueOf(1));
143        cstags.put("HSV ", Integer.valueOf(3));
144        cstags.put("HLS ", Integer.valueOf(3));
145        cstags.put("CMYK", Integer.valueOf(4));
146        cstags.put("CMY ", Integer.valueOf(3));
147        cstags.put("2CLR", Integer.valueOf(2));
148        cstags.put("3CLR", Integer.valueOf(3));
149        cstags.put("4CLR", Integer.valueOf(4));
150        cstags.put("5CLR", Integer.valueOf(5));
151        cstags.put("6CLR", Integer.valueOf(6));
152        cstags.put("7CLR", Integer.valueOf(7));
153        cstags.put("8CLR", Integer.valueOf(8));
154        cstags.put("9CLR", Integer.valueOf(9));
155        cstags.put("ACLR", Integer.valueOf(10));
156        cstags.put("BCLR", Integer.valueOf(11));
157        cstags.put("CCLR", Integer.valueOf(12));
158        cstags.put("DCLR", Integer.valueOf(13));
159        cstags.put("ECLR", Integer.valueOf(14));
160        cstags.put("FCLR", Integer.valueOf(15));
161    }
162}