blob: bddbc2040b2e8ff49757997c9e82fcb110e0a506 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* $Id$
* Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.image.analyser;
// Java
import java.io.BufferedInputStream;
import java.io.IOException;
// FOP
import org.apache.fop.image.FopImage;
import org.apache.fop.fo.FOUserAgent;
/**
* ImageReader object for JPEG image type.
*
* @author Pankaj Narula
* @version $Id$
*/
public class JPEGReader implements ImageReader {
/**
* Only SOFn and APPn markers are defined as SOFn is needed for the height and
* width search. APPn is also defined because if the JPEG contains thumbnails
* the dimensions of the thumnail would also be after the SOFn marker enclosed
* inside the APPn marker. And we don't want to confuse those dimensions with
* the image dimensions.
*/
private static final int MARK = 0xff; // Beginning of a Marker
private static final int NULL = 0x00; // Special case for 0xff00
private static final int SOF1 = 0xc0; // Baseline DCT
private static final int SOF2 = 0xc1; // Extended Sequential DCT
private static final int SOF3 = 0xc2; // Progrssive DCT only PDF 1.3
private static final int SOFA = 0xca; // Progressice DCT only PDF 1.3
private static final int APP0 = 0xe0; // Application marker, JFIF
private static final int APPF = 0xef; // Application marker
private static final int SOS = 0xda; // Start of Scan
private static final int SOI = 0xd8; // start of Image
private static final int JPG_SIG_LENGTH = 2;
/** @see org.apache.fop.image.analyser.ImageReader */
public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis,
FOUserAgent ua) throws IOException {
byte[] header = getDefaultHeader(fis);
boolean supported = ((header[0] == (byte) 0xff)
&& (header[1] == (byte) 0xd8));
if (supported) {
FopImage.ImageInfo info = getDimension(fis);
info.mimeType = getMimeType();
return info;
} else {
return null;
}
}
/**
* Returns the MIME type supported by this implementation.
*
* @return The MIME type
*/
public String getMimeType() {
return "image/jpeg";
}
private byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException {
byte[] header = new byte[JPG_SIG_LENGTH];
try {
imageStream.mark(JPG_SIG_LENGTH + 1);
imageStream.read(header);
imageStream.reset();
} catch (IOException ex) {
try {
imageStream.reset();
} catch (IOException exbis) {
// throw the original exception, not this one
}
throw ex;
}
return header;
}
private FopImage.ImageInfo getDimension(BufferedInputStream imageStream) throws IOException {
FopImage.ImageInfo info = new FopImage.ImageInfo();
try {
int marker = NULL;
long length, skipped;
outer:
while (imageStream.available() > 0) {
while ((marker = imageStream.read()) != MARK) {
//nop, simply skip
}
do {
marker = imageStream.read();
} while (marker == MARK);
switch (marker) {
case SOI:
break;
case NULL:
break;
case SOF1:
case SOF2:
case SOF3: // SOF3 and SOFA are only supported by PDF 1.3
case SOFA:
this.skip(imageStream, 3);
info.height = this.read2bytes(imageStream);
info.width = this.read2bytes(imageStream);
break outer;
default:
length = this.read2bytes(imageStream);
skipped = this.skip(imageStream, length - 2);
if (skipped != length - 2) {
throw new IOException("Skipping Error");
}
}
}
} catch (IOException ioe) {
try {
imageStream.reset();
} catch (IOException exbis) {
// throw the original exception, not this one
}
throw ioe;
}
return info;
}
private int read2bytes(BufferedInputStream imageStream) throws IOException {
int byte1 = imageStream.read();
int byte2 = imageStream.read();
return (int) ((byte1 << 8) | byte2);
}
private long skip(BufferedInputStream imageStream, long n) throws IOException {
long discarded = 0;
while (discarded != n) {
imageStream.read();
discarded++;
}
return discarded; // scope for exception
}
}
|