| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 47 |
|
| 48 |
public ImageIntrinsics(String imageFn) { |
| 49 |
System.setProperty("java.awt.headless","true"); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 81 |
} |
| 82 |
width = image.getWidth(this); |
| 83 |
depth = image.getHeight(this); |
| 84 |
} |
| 85 |
|
| 86 |
if (imageFailed) { |
| 87 |
|
| 88 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
int flags = ImageObserver.ALLBITS; |
| 183 |
if ((infoflags & flags) == flags) { |
| 184 |
return false; |
| 185 |
} else { |
| 186 |
return true; |
| 187 |
} |
| 188 |
} |
| 189 |
} |