Jpeg2000.java

1
/*
2
 * $Id: Jpeg2000.java 4065 2009-09-16 23:09:11Z psoares33 $
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
    
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
     */
99
    public Jpeg2000(URL url) throws BadElementException, IOException {
100
        super(url);
101 1 1. : removed call to com/lowagie/text/Jpeg2000::processParameters → NO_COVERAGE
        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
    
112
    public Jpeg2000(byte[] img) throws BadElementException, IOException {
113
        super((URL)null);
114
        rawData = img;
115
        originalData = img;
116 1 1. : removed call to com/lowagie/text/Jpeg2000::processParameters → NO_COVERAGE
        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
    
129
    public Jpeg2000(byte[] img, float width, float height) throws BadElementException, IOException {
130
        this(img);
131
        scaledWidth = width;
132
        scaledHeight = height;
133
    }
134
    
135
    private int cio_read(int n) throws IOException {
136
        int v = 0;
137 4 1. cio_read : changed conditional boundary → NO_COVERAGE
2. cio_read : Changed increment from -1 to 1 → NO_COVERAGE
3. cio_read : Replaced integer subtraction with addition → NO_COVERAGE
4. cio_read : negated conditional → NO_COVERAGE
        for (int i = n - 1; i >= 0; i--) {
138 3 1. cio_read : Replaced Shift Left with Shift Right → NO_COVERAGE
2. cio_read : Replaced Shift Left with Shift Right → NO_COVERAGE
3. cio_read : Replaced integer addition with subtraction → NO_COVERAGE
            v += inp.read() << (i << 3);
139
        }
140 1 1. cio_read : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
        return v;
141
    }
142
    
143
    public void jp2_read_boxhdr() throws IOException {
144
        boxLength = cio_read(4);
145
        boxType = cio_read(4);
146 1 1. jp2_read_boxhdr : negated conditional → NO_COVERAGE
        if (boxLength == 1) {
147 1 1. jp2_read_boxhdr : negated conditional → NO_COVERAGE
            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 1 1. jp2_read_boxhdr : negated conditional → NO_COVERAGE
            if (boxLength == 0) 
152
                throw new IOException(MessageLocalization.getComposedMessage("unsupported.box.size.eq.eq.0"));
153
        }
154 1 1. jp2_read_boxhdr : negated conditional → NO_COVERAGE
        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
     */
163
    private void processParameters() throws IOException {
164
        type = JPEG2000;
165
        originalType = ORIGINAL_JPEG2000;
166
        inp = null;
167
        try {
168
            String errorID;
169 1 1. processParameters : negated conditional → NO_COVERAGE
            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 1 1. processParameters : negated conditional → NO_COVERAGE
            if (boxLength == 0x0000000c) {
179
                boxType = cio_read(4);
180 1 1. processParameters : negated conditional → NO_COVERAGE
                if (JP2_JP != boxType) {
181
                    throw new IOException(MessageLocalization.getComposedMessage("expected.jp.marker"));
182
                }
183 1 1. processParameters : negated conditional → NO_COVERAGE
                if (0x0d0a870a != cio_read(4)) {
184
                    throw new IOException(MessageLocalization.getComposedMessage("error.with.jp.marker"));
185
                }
186
187 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE
                jp2_read_boxhdr();
188 1 1. processParameters : negated conditional → NO_COVERAGE
                if (JP2_FTYP != boxType) {
189
                    throw new IOException(MessageLocalization.getComposedMessage("expected.ftyp.marker"));
190
                }
191 2 1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE
2. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE
                Utilities.skip(inp, boxLength - 8);
192 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE
                jp2_read_boxhdr();
193
                do {
194 1 1. processParameters : negated conditional → NO_COVERAGE
                    if (JP2_JP2H != boxType) {
195 1 1. processParameters : negated conditional → NO_COVERAGE
                        if (boxType == JP2_JP2C) {
196
                            throw new IOException(MessageLocalization.getComposedMessage("expected.jp2h.marker"));
197
                        }
198 2 1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE
2. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE
                        Utilities.skip(inp, boxLength - 8);
199 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE
                        jp2_read_boxhdr();
200
                    }
201 1 1. processParameters : negated conditional → NO_COVERAGE
                } while(JP2_JP2H != boxType);
202 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE
                jp2_read_boxhdr();
203 1 1. processParameters : negated conditional → NO_COVERAGE
                if (JP2_IHDR != boxType) {
204
                    throw new IOException(MessageLocalization.getComposedMessage("expected.ihdr.marker"));
205
                }
206
                scaledHeight = cio_read(4);
207 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::setTop → NO_COVERAGE
                setTop(scaledHeight);
208
                scaledWidth = cio_read(4);
209 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::setRight → NO_COVERAGE
                setRight(scaledWidth);
210
                bpc = -1;
211
            }
212 1 1. processParameters : negated conditional → NO_COVERAGE
            else if (boxLength == 0xff4fff51) {
213 1 1. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE
                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 1 1. processParameters : removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE
                Utilities.skip(inp, 16);
219
                colorspace = cio_read(2);
220
                bpc = 8;
221 1 1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE
                scaledHeight = y1 - y0;
222 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::setTop → NO_COVERAGE
                setTop(scaledHeight);
223 1 1. processParameters : Replaced integer subtraction with addition → NO_COVERAGE
                scaledWidth = x1 - x0;
224 1 1. processParameters : removed call to com/lowagie/text/Jpeg2000::setRight → NO_COVERAGE
                setRight(scaledWidth);
225
            }
226
            else {
227
                throw new IOException(MessageLocalization.getComposedMessage("not.a.valid.jpeg2000.file"));
228
            }
229
        }
230
        finally {
231 1 1. processParameters : negated conditional → NO_COVERAGE
            if (inp != null) {
232 1 1. processParameters : removed call to java/io/InputStream::close → NO_COVERAGE
                try{inp.close();}catch(Exception e){}
233
                inp = null;
234
            }
235
        }
236
        plainWidth = getWidth();
237
        plainHeight = getHeight();
238
    }
239
}

Mutations

101

1.1
Location :
Killed by : none
removed call to com/lowagie/text/Jpeg2000::processParameters → NO_COVERAGE

116

1.1
Location :
Killed by : none
removed call to com/lowagie/text/Jpeg2000::processParameters → NO_COVERAGE

137

1.1
Location : cio_read
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : cio_read
Killed by : none
Changed increment from -1 to 1 → NO_COVERAGE

3.3
Location : cio_read
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

4.4
Location : cio_read
Killed by : none
negated conditional → NO_COVERAGE

138

1.1
Location : cio_read
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

2.2
Location : cio_read
Killed by : none
Replaced Shift Left with Shift Right → NO_COVERAGE

3.3
Location : cio_read
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

140

1.1
Location : cio_read
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

146

1.1
Location : jp2_read_boxhdr
Killed by : none
negated conditional → NO_COVERAGE

147

1.1
Location : jp2_read_boxhdr
Killed by : none
negated conditional → NO_COVERAGE

151

1.1
Location : jp2_read_boxhdr
Killed by : none
negated conditional → NO_COVERAGE

154

1.1
Location : jp2_read_boxhdr
Killed by : none
negated conditional → NO_COVERAGE

169

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

178

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

180

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

183

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

187

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE

188

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

191

1.1
Location : processParameters
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE

192

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE

194

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

195

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

198

1.1
Location : processParameters
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE

199

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE

201

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

202

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::jp2_read_boxhdr → NO_COVERAGE

203

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

207

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::setTop → NO_COVERAGE

209

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::setRight → NO_COVERAGE

212

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

213

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE

218

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Utilities::skip → NO_COVERAGE

221

1.1
Location : processParameters
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

222

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::setTop → NO_COVERAGE

223

1.1
Location : processParameters
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

224

1.1
Location : processParameters
Killed by : none
removed call to com/lowagie/text/Jpeg2000::setRight → NO_COVERAGE

231

1.1
Location : processParameters
Killed by : none
negated conditional → NO_COVERAGE

232

1.1
Location : processParameters
Killed by : none
removed call to java/io/InputStream::close → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.4.2