1 /*
2  * $Id$
3  *
4  * Copyright 2007 by Paulo Soares.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text;
51 
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.net.URL;
55 import com.lowagie.text.error_messages.MessageLocalization;
56 
57 /**
58  * An <CODE>Jpeg2000</CODE> is the representation of a graphic element (JPEG)
59  * that has to be inserted into the document
60  *
61  * @see		Element
62  * @see		Image
63  */
64 
65 public class Jpeg2000 extends Image {
66 
67     // public static final membervariables
68 
69     public static final int JP2_JP = 0x6a502020;
70     public static final int JP2_IHDR = 0x69686472;
71     public static final int JPIP_JPIP = 0x6a706970;
72 
73     public static final int JP2_FTYP = 0x66747970;
74     public static final int JP2_JP2H = 0x6a703268;
75     public static final int JP2_COLR = 0x636f6c72;
76     public static final int JP2_JP2C = 0x6a703263;
77     public static final int JP2_URL = 0x75726c20;
78     public static final int JP2_DBTL = 0x6474626c;
79     public static final int JP2_BPCC = 0x62706363;
80     public static final int JP2_JP2 = 0x6a703220;
81 
82     InputStream inp;
83     int boxLength;
84     int boxType;
85 
86     // Constructors
87 
Jpeg2000(Image image)88     Jpeg2000(Image image) {
89         super(image);
90     }
91 
92     /**
93      * Constructs a <CODE>Jpeg2000</CODE>-object, using an <VAR>url</VAR>.
94      *
95      * @param		url			the <CODE>URL</CODE> where the image can be found
96      * @throws BadElementException
97      * @throws IOException
98      */
Jpeg2000(URL url)99     public Jpeg2000(URL url) throws BadElementException, IOException {
100         super(url);
101         processParameters();
102     }
103 
104     /**
105      * Constructs a <CODE>Jpeg2000</CODE>-object from memory.
106      *
107      * @param		img		the memory image
108      * @throws BadElementException
109      * @throws IOException
110      */
111 
Jpeg2000(byte[] img)112     public Jpeg2000(byte[] img) throws BadElementException, IOException {
113         super((URL)null);
114         rawData = img;
115         originalData = img;
116         processParameters();
117     }
118 
119     /**
120      * Constructs a <CODE>Jpeg2000</CODE>-object from memory.
121      *
122      * @param		img			the memory image.
123      * @param		width		the width you want the image to have
124      * @param		height		the height you want the image to have
125      * @throws BadElementException
126      * @throws IOException
127      */
128 
Jpeg2000(byte[] img, float width, float height)129     public Jpeg2000(byte[] img, float width, float height) throws BadElementException, IOException {
130         this(img);
131         scaledWidth = width;
132         scaledHeight = height;
133     }
134 
cio_read(int n)135     private int cio_read(int n) throws IOException {
136         int v = 0;
137         for (int i = n - 1; i >= 0; i--) {
138             v += inp.read() << (i << 3);
139         }
140         return v;
141     }
142 
jp2_read_boxhdr()143     public void jp2_read_boxhdr() throws IOException {
144         boxLength = cio_read(4);
145         boxType = cio_read(4);
146         if (boxLength == 1) {
147             if (cio_read(4) != 0) {
148                 throw new IOException(MessageLocalization.getComposedMessage("cannot.handle.box.sizes.higher.than.2.32"));
149             }
150             boxLength = cio_read(4);
151             if (boxLength == 0)
152                 throw new IOException(MessageLocalization.getComposedMessage("unsupported.box.size.eq.eq.0"));
153         }
154         else if (boxLength == 0) {
155             throw new IOException(MessageLocalization.getComposedMessage("unsupported.box.size.eq.eq.0"));
156         }
157     }
158 
159     /**
160      * This method checks if the image is a valid JPEG and processes some parameters.
161      * @throws IOException
162      */
processParameters()163     private void processParameters() throws IOException {
164         type = JPEG2000;
165         originalType = ORIGINAL_JPEG2000;
166         inp = null;
167         try {
168             String errorID;
169             if (rawData == null){
170                 inp = url.openStream();
171                 errorID = url.toString();
172             }
173             else{
174                 inp = new java.io.ByteArrayInputStream(rawData);
175                 errorID = "Byte array";
176             }
177             boxLength = cio_read(4);
178             if (boxLength == 0x0000000c) {
179                 boxType = cio_read(4);
180                 if (JP2_JP != boxType) {
181                     throw new IOException(MessageLocalization.getComposedMessage("expected.jp.marker"));
182                 }
183                 if (0x0d0a870a != cio_read(4)) {
184                     throw new IOException(MessageLocalization.getComposedMessage("error.with.jp.marker"));
185                 }
186 
187                 jp2_read_boxhdr();
188                 if (JP2_FTYP != boxType) {
189                     throw new IOException(MessageLocalization.getComposedMessage("expected.ftyp.marker"));
190                 }
191                 Utilities.skip(inp, boxLength - 8);
192                 jp2_read_boxhdr();
193                 do {
194                     if (JP2_JP2H != boxType) {
195                         if (boxType == JP2_JP2C) {
196                             throw new IOException(MessageLocalization.getComposedMessage("expected.jp2h.marker"));
197                         }
198                         Utilities.skip(inp, boxLength - 8);
199                         jp2_read_boxhdr();
200                     }
201                 } while(JP2_JP2H != boxType);
202                 jp2_read_boxhdr();
203                 if (JP2_IHDR != boxType) {
204                     throw new IOException(MessageLocalization.getComposedMessage("expected.ihdr.marker"));
205                 }
206                 scaledHeight = cio_read(4);
207                 setTop(scaledHeight);
208                 scaledWidth = cio_read(4);
209                 setRight(scaledWidth);
210                 bpc = -1;
211             }
212             else if (boxLength == 0xff4fff51) {
213                 Utilities.skip(inp, 4);
214                 int x1 = cio_read(4);
215                 int y1 = cio_read(4);
216                 int x0 = cio_read(4);
217                 int y0 = cio_read(4);
218                 Utilities.skip(inp, 16);
219                 colorspace = cio_read(2);
220                 bpc = 8;
221                 scaledHeight = y1 - y0;
222                 setTop(scaledHeight);
223                 scaledWidth = x1 - x0;
224                 setRight(scaledWidth);
225             }
226             else {
227                 throw new IOException(MessageLocalization.getComposedMessage("not.a.valid.jpeg2000.file"));
228             }
229         }
230         finally {
231             if (inp != null) {
232                 try{inp.close();}catch(Exception e){}
233                 inp = null;
234             }
235         }
236         plainWidth = getWidth();
237         plainHeight = getHeight();
238     }
239 }
240