001/* 002 * $Id: Jpeg2000.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; 045 046import java.io.IOException; 047import java.io.InputStream; 048import java.net.URL; 049import com.itextpdf.text.error_messages.MessageLocalization; 050 051/** 052 * An <CODE>Jpeg2000</CODE> is the representation of a graphic element (JPEG) 053 * that has to be inserted into the document 054 * 055 * @see Element 056 * @see Image 057 */ 058 059public class Jpeg2000 extends Image { 060 061 // public static final membervariables 062 063 public static final int JP2_JP = 0x6a502020; 064 public static final int JP2_IHDR = 0x69686472; 065 public static final int JPIP_JPIP = 0x6a706970; 066 067 public static final int JP2_FTYP = 0x66747970; 068 public static final int JP2_JP2H = 0x6a703268; 069 public static final int JP2_COLR = 0x636f6c72; 070 public static final int JP2_JP2C = 0x6a703263; 071 public static final int JP2_URL = 0x75726c20; 072 public static final int JP2_DBTL = 0x6474626c; 073 public static final int JP2_BPCC = 0x62706363; 074 public static final int JP2_JP2 = 0x6a703220; 075 076 InputStream inp; 077 int boxLength; 078 int boxType; 079 080 // Constructors 081 082 Jpeg2000(Image image) { 083 super(image); 084 } 085 086 /** 087 * Constructs a <CODE>Jpeg2000</CODE>-object, using an <VAR>url</VAR>. 088 * 089 * @param url the <CODE>URL</CODE> where the image can be found 090 * @throws BadElementException 091 * @throws IOException 092 */ 093 public Jpeg2000(URL url) throws BadElementException, IOException { 094 super(url); 095 processParameters(); 096 } 097 098 /** 099 * Constructs a <CODE>Jpeg2000</CODE>-object from memory. 100 * 101 * @param img the memory image 102 * @throws BadElementException 103 * @throws IOException 104 */ 105 106 public Jpeg2000(byte[] img) throws BadElementException, IOException { 107 super((URL)null); 108 rawData = img; 109 originalData = img; 110 processParameters(); 111 } 112 113 /** 114 * Constructs a <CODE>Jpeg2000</CODE>-object from memory. 115 * 116 * @param img the memory image. 117 * @param width the width you want the image to have 118 * @param height the height you want the image to have 119 * @throws BadElementException 120 * @throws IOException 121 */ 122 123 public Jpeg2000(byte[] img, float width, float height) throws BadElementException, IOException { 124 this(img); 125 scaledWidth = width; 126 scaledHeight = height; 127 } 128 129 private int cio_read(int n) throws IOException { 130 int v = 0; 131 for (int i = n - 1; i >= 0; i--) { 132 v += inp.read() << (i << 3); 133 } 134 return v; 135 } 136 137 public void jp2_read_boxhdr() throws IOException { 138 boxLength = cio_read(4); 139 boxType = cio_read(4); 140 if (boxLength == 1) { 141 if (cio_read(4) != 0) { 142 throw new IOException(MessageLocalization.getComposedMessage("cannot.handle.box.sizes.higher.than.2.32")); 143 } 144 boxLength = cio_read(4); 145 if (boxLength == 0) 146 throw new IOException(MessageLocalization.getComposedMessage("unsupported.box.size.eq.eq.0")); 147 } 148 else if (boxLength == 0) { 149 throw new IOException(MessageLocalization.getComposedMessage("unsupported.box.size.eq.eq.0")); 150 } 151 } 152 153 /** 154 * This method checks if the image is a valid JPEG and processes some parameters. 155 * @throws IOException 156 */ 157 private void processParameters() throws IOException { 158 type = JPEG2000; 159 originalType = ORIGINAL_JPEG2000; 160 inp = null; 161 try { 162 String errorID; 163 if (rawData == null){ 164 inp = url.openStream(); 165 errorID = url.toString(); 166 } 167 else{ 168 inp = new java.io.ByteArrayInputStream(rawData); 169 errorID = "Byte array"; 170 } 171 boxLength = cio_read(4); 172 if (boxLength == 0x0000000c) { 173 boxType = cio_read(4); 174 if (JP2_JP != boxType) { 175 throw new IOException(MessageLocalization.getComposedMessage("expected.jp.marker")); 176 } 177 if (0x0d0a870a != cio_read(4)) { 178 throw new IOException(MessageLocalization.getComposedMessage("error.with.jp.marker")); 179 } 180 181 jp2_read_boxhdr(); 182 if (JP2_FTYP != boxType) { 183 throw new IOException(MessageLocalization.getComposedMessage("expected.ftyp.marker")); 184 } 185 Utilities.skip(inp, boxLength - 8); 186 jp2_read_boxhdr(); 187 do { 188 if (JP2_JP2H != boxType) { 189 if (boxType == JP2_JP2C) { 190 throw new IOException(MessageLocalization.getComposedMessage("expected.jp2h.marker")); 191 } 192 Utilities.skip(inp, boxLength - 8); 193 jp2_read_boxhdr(); 194 } 195 } while(JP2_JP2H != boxType); 196 jp2_read_boxhdr(); 197 if (JP2_IHDR != boxType) { 198 throw new IOException(MessageLocalization.getComposedMessage("expected.ihdr.marker")); 199 } 200 scaledHeight = cio_read(4); 201 setTop(scaledHeight); 202 scaledWidth = cio_read(4); 203 setRight(scaledWidth); 204 bpc = -1; 205 } 206 else if (boxLength == 0xff4fff51) { 207 Utilities.skip(inp, 4); 208 int x1 = cio_read(4); 209 int y1 = cio_read(4); 210 int x0 = cio_read(4); 211 int y0 = cio_read(4); 212 Utilities.skip(inp, 16); 213 colorspace = cio_read(2); 214 bpc = 8; 215 scaledHeight = y1 - y0; 216 setTop(scaledHeight); 217 scaledWidth = x1 - x0; 218 setRight(scaledWidth); 219 } 220 else { 221 throw new IOException(MessageLocalization.getComposedMessage("not.a.valid.jpeg2000.file")); 222 } 223 } 224 finally { 225 if (inp != null) { 226 try{inp.close();}catch(Exception e){} 227 inp = null; 228 } 229 } 230 plainWidth = getWidth(); 231 plainHeight = getHeight(); 232 } 233}