Source code: com/nwalsh/saxon/ImageIntrinsics.java
1 package com.nwalsh.saxon;
2
3 import java.io.*;
4 import java.awt.*;
5 import java.awt.image.*;
6 import java.lang.Thread;
7 import java.util.StringTokenizer;
8
9 /**
10 * <p>Saxon extension to examine intrinsic size of images</p>
11 *
12 * <p>$Id: ImageIntrinsics.java,v 1.1 2002/05/08 02:12:29 nwalsh Exp $</p>
13 *
14 * <p>Copyright (C) 2002 Norman Walsh.</p>
15 *
16 * <p>This class provides a
17 * <a href="http://saxon.sourceforge.net/">Saxon</a>
18 * extension to find the intrinsic size of images.</p>
19 *
20 * <p><b>Change Log:</b></p>
21 * <dl>
22 * <dt>1.0</dt>
23 * <dd><p>Initial release.</p></dd>
24 * </dl>
25 *
26 * @author Norman Walsh
27 * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
28 *
29 * @version $Id: ImageIntrinsics.java,v 1.1 2002/05/08 02:12:29 nwalsh Exp $
30 *
31 */
32 public class ImageIntrinsics implements ImageObserver {
33 boolean imageLoaded = false;
34 boolean imageFailed = false;
35 Image image = null;
36 int width = -1;
37 int depth = -1;
38
39 /**
40 * <p>Constructor for ImageIntrinsics</p>
41 */
42 public ImageIntrinsics(String imageFn) {
43 image = Toolkit.getDefaultToolkit().getImage (imageFn);
44 width = image.getWidth(this);
45
46 while (!imageFailed && (width == -1 || depth == -1)) {
47 try {
48 java.lang.Thread.currentThread().sleep(50);
49 } catch (Exception e) {
50 // nop;
51 }
52 width = image.getWidth(this);
53 depth = image.getHeight(this);
54 }
55
56 if (imageFailed) {
57 // Maybe it's an EPS or PDF?
58 // FIXME: this code is crude
59 BufferedReader ir = null;
60 String line = null;
61 int lineLimit = 100;
62
63 try {
64 ir = new BufferedReader(new FileReader(new File(imageFn)));
65 line = ir.readLine();
66
67 if (line != null && line.startsWith("%PDF-")) {
68 // We've got a PDF!
69 while (lineLimit > 0 && line != null) {
70 lineLimit--;
71 if (line.startsWith("/CropBox [")) {
72 line = line.substring(10);
73 if (line.indexOf("]") >= 0) {
74 line = line.substring(0, line.indexOf("]"));
75 }
76 parseBox(line);
77 lineLimit = 0;
78 }
79 line = ir.readLine();
80 }
81 } else if (line != null && line.startsWith("%!") && line.indexOf(" EPSF-") > 0) {
82 // We've got an EPS!
83 while (lineLimit > 0 && line != null) {
84 lineLimit--;
85 if (line.startsWith("%%BoundingBox: ")) {
86 line = line.substring(15);
87 parseBox(line);
88 lineLimit = 0;
89 }
90 line = ir.readLine();
91 }
92 }
93 } catch (Exception e) {
94 // nop;
95 }
96
97 if (ir != null) {
98 try {
99 ir.close();
100 } catch (Exception e) {
101 // nop;
102 }
103 }
104 }
105 }
106
107 public int getWidth(int defaultWidth) {
108 if (width >= 0) {
109 return width;
110 } else {
111 return defaultWidth;
112 }
113 }
114
115 public int getDepth(int defaultDepth) {
116 if (depth >= 0) {
117 return depth;
118 } else {
119 return defaultDepth;
120 }
121 }
122
123 private void parseBox(String line) {
124 int [] corners = new int [4];
125 int count = 0;
126
127 StringTokenizer st = new StringTokenizer(line);
128 while (count < 4 && st.hasMoreTokens()) {
129 try {
130 corners[count++] = Integer.parseInt(st.nextToken());
131 } catch (Exception e) {
132 // nop;
133 }
134 }
135
136 width = corners[2] - corners[0];
137 depth = corners[3] - corners[1];
138 }
139
140 public boolean imageUpdate(Image img, int infoflags,
141 int x, int y, int width, int height) {
142 if ((infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) {
143 imageFailed = true;
144 return false;
145 }
146
147 // I really only care about the width and height, but if I return false as
148 // soon as those are available, the BufferedInputStream behind the loader
149 // gets closed too early.
150 int flags = ImageObserver.ALLBITS;
151 if ((infoflags & flags) == flags) {
152 return false;
153 } else {
154 return true;
155 }
156 }
157 }