Debian Codegraph @

src/com/nwalsh/saxon/ImageIntrinsics.java

raw symbols SCIP
1 package com.nwalsh.saxon;
2
3 import java.io.File;
4 import java.io.BufferedReader;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.net.MalformedURLException;
9 import java.awt.Toolkit;
10 import java.awt.Image;
11 import java.awt.image.ImageObserver;
12 import java.lang.Thread;
13 import java.util.StringTokenizer;
14
15 /**
16 * <p>Saxon extension to examine intrinsic size of images</p>
17 *
18 * <p>$Id: ImageIntrinsics.java 5480 2005-12-06 18:56:52Z kosek $</p>
19 *
20 * <p>Copyright (C) 2002 Norman Walsh.</p>
21 *
22 * <p>This class provides a
23 * <a href="http://saxon.sourceforge.net/">Saxon</a>
24 * extension to find the intrinsic size of images.</p>
25 *
26 * <p><b>Change Log:</b></p>
27 * <dl>
28 * <dt>1.0</dt>
29 * <dd><p>Initial release.</p></dd>
30 * </dl>
31 *
32 * @author Norman Walsh
33 * <a href="mailto:[email protected]">[email protected]</a>
34 *
35 * @version $Id: ImageIntrinsics.java 5480 2005-12-06 18:56:52Z kosek $
36 *
37 */
38 public class ImageIntrinsics implements ImageObserver {
39 boolean imageLoaded = false;
40 boolean imageFailed = false;
41 Image image = null;
42 int width = -1;
43 int depth = -1;
44
45 /**
46 * <p>Constructor for ImageIntrinsics</p>
47 */
48 public ImageIntrinsics(String imageFn) {
49 System.setProperty("java.awt.headless","true");
50
51 // Hack. I expect the right way to do this is to always use a URL.
52 // However, that means getting the base URI correct and dealing
53 // with the various permutations of the file: URI scheme on different
54 // platforms. So instead, what we're going to do is cheat. If it
55 // starts with http: or ftp:, call it a URI. Otherwise, call it
56 // a file. Also call it a file if the URI is malformed.
57
58 URL imageUrl = null;
59
60 if (imageFn.startsWith("http:") || imageFn.startsWith("ftp:") || imageFn.startsWith("file:")) {
61 try {
62 imageUrl = new URL(imageFn);
63 } catch (MalformedURLException mue) {
64 imageUrl = null;
65 }
66 }
67
68 if (imageUrl != null) {
69 image = Toolkit.getDefaultToolkit().getImage (imageUrl);
70 } else {
71 image = Toolkit.getDefaultToolkit().getImage (imageFn);
72 }
73
74 width = image.getWidth(this);
75
76 while (!imageFailed && (width == -1 || depth == -1)) {
77 try {
78 java.lang.Thread.currentThread().sleep(50);
79 } catch (Exception e) {
80 // nop;
81 }
82 width = image.getWidth(this);
83 depth = image.getHeight(this);
84 }
85
86 if (imageFailed) {
87 // Maybe it's an EPS or PDF?
88 // FIXME: this code is crude (and doesn't handle the URL case!!!)
89 BufferedReader ir = null;
90 String line = null;
91 int lineLimit = 100;
92
93 try {
94 ir = new BufferedReader(new FileReader(new File(imageFn)));
95 line = ir.readLine();
96
97 if (line != null && line.startsWith("%PDF-")) {
98 // We've got a PDF!
99 while (lineLimit > 0 && line != null) {
100 lineLimit--;
101 if (line.startsWith("/CropBox [")) {
102 line = line.substring(10);
103 if (line.indexOf("]") >= 0) {
104 line = line.substring(0, line.indexOf("]"));
105 }
106 parseBox(line);
107 lineLimit = 0;
108 }
109 line = ir.readLine();
110 }
111 } else if (line != null && line.startsWith("%!") && line.indexOf(" EPSF-") > 0) {
112 // We've got an EPS!
113 while (lineLimit > 0 && line != null) {
114 lineLimit--;
115 if (line.startsWith("%%BoundingBox: ")) {
116 line = line.substring(15);
117 parseBox(line);
118 lineLimit = 0;
119 }
120 line = ir.readLine();
121 }
122 } else {
123 System.err.println("Failed to interpret image: " + imageFn);
124 }
125 } catch (Exception e) {
126 System.err.println("Failed to load image: " + imageFn);
127 }
128
129 if (ir != null) {
130 try {
131 ir.close();
132 } catch (Exception e) {
133 // nop;
134 }
135 }
136 }
137 }
138
139 public int getWidth(int defaultWidth) {
140 if (width >= 0) {
141 return width;
142 } else {
143 return defaultWidth;
144 }
145 }
146
147 public int getDepth(int defaultDepth) {
148 if (depth >= 0) {
149 return depth;
150 } else {
151 return defaultDepth;
152 }
153 }
154
155 private void parseBox(String line) {
156 int [] corners = new int [4];
157 int count = 0;
158
159 StringTokenizer st = new StringTokenizer(line);
160 while (count < 4 && st.hasMoreTokens()) {
161 try {
162 corners[count++] = Integer.parseInt(st.nextToken());
163 } catch (Exception e) {
164 // nop;
165 }
166 }
167
168 width = corners[2] - corners[0];
169 depth = corners[3] - corners[1];
170 }
171
172 public boolean imageUpdate(Image img, int infoflags,
173 int x, int y, int width, int height) {
174 if ((infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) {
175 imageFailed = true;
176 return false;
177 }
178
179 // I really only care about the width and height, but if I return false as
180 // soon as those are available, the BufferedInputStream behind the loader
181 // gets closed too early.
182 int flags = ImageObserver.ALLBITS;
183 if ((infoflags & flags) == flags) {
184 return false;
185 } else {
186 return true;
187 }
188 }
189 }