From: Norman Walsh Date: Wed, 8 May 2002 02:12:29 +0000 (+0000) Subject: New extensions to determine the size of an image X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4debbc68c39a7b224211724cc7ffefc19c94a1a1;p=docbook-dsssl New extensions to determine the size of an image --- diff --git a/xsl/extensions/saxon643/.classes/Makefile.incl b/xsl/extensions/saxon643/.classes/Makefile.incl index d2917c8f5..db567cb78 100644 --- a/xsl/extensions/saxon643/.classes/Makefile.incl +++ b/xsl/extensions/saxon643/.classes/Makefile.incl @@ -1 +1 @@ -CLASSFILES= \ No newline at end of file +CLASSFILES= diff --git a/xsl/extensions/saxon643/.classes/com/Makefile.incl b/xsl/extensions/saxon643/.classes/com/Makefile.incl index d2917c8f5..db567cb78 100644 --- a/xsl/extensions/saxon643/.classes/com/Makefile.incl +++ b/xsl/extensions/saxon643/.classes/com/Makefile.incl @@ -1 +1 @@ -CLASSFILES= \ No newline at end of file +CLASSFILES= diff --git a/xsl/extensions/saxon643/.classes/com/nwalsh/Makefile.incl b/xsl/extensions/saxon643/.classes/com/nwalsh/Makefile.incl index d2917c8f5..db567cb78 100644 --- a/xsl/extensions/saxon643/.classes/com/nwalsh/Makefile.incl +++ b/xsl/extensions/saxon643/.classes/com/nwalsh/Makefile.incl @@ -1 +1 @@ -CLASSFILES= \ No newline at end of file +CLASSFILES= diff --git a/xsl/extensions/saxon643/.classes/com/nwalsh/saxon/Makefile.incl b/xsl/extensions/saxon643/.classes/com/nwalsh/saxon/Makefile.incl index f7d7b42a2..de568b476 100644 --- a/xsl/extensions/saxon643/.classes/com/nwalsh/saxon/Makefile.incl +++ b/xsl/extensions/saxon643/.classes/com/nwalsh/saxon/Makefile.incl @@ -1,16 +1,18 @@ -CLASSFILES=CVS.class \ -Callout.class \ +CLASSFILES=Callout.class \ CalloutEmitter.class \ ColumnScanEmitter.class \ ColumnUpdateEmitter.class \ CopyEmitter.class \ +CVS.class \ FormatCallout.class \ FormatGraphicCallout.class \ FormatTextCallout.class \ FormatUnicodeCallout.class \ +ImageIntrinsics.class \ LineCountEmitter.class \ NumberLinesEmitter.class \ Table.class \ Text.class \ TextFactory.class \ Verbatim.class + diff --git a/xsl/extensions/saxon643/com/nwalsh/saxon/ImageIntrinsics.java b/xsl/extensions/saxon643/com/nwalsh/saxon/ImageIntrinsics.java new file mode 100644 index 000000000..c538ab11d --- /dev/null +++ b/xsl/extensions/saxon643/com/nwalsh/saxon/ImageIntrinsics.java @@ -0,0 +1,157 @@ +package com.nwalsh.saxon; + +import java.io.*; +import java.awt.*; +import java.awt.image.*; +import java.lang.Thread; +import java.util.StringTokenizer; + +/** + *

Saxon extension to examine intrinsic size of images

+ * + *

$Id$

+ * + *

Copyright (C) 2002 Norman Walsh.

+ * + *

This class provides a + * Saxon + * extension to find the intrinsic size of images.

+ * + *

Change Log:

+ *
+ *
1.0
+ *

Initial release.

+ *
+ * + * @author Norman Walsh + * ndw@nwalsh.com + * + * @version $Id$ + * + */ +public class ImageIntrinsics implements ImageObserver { + boolean imageLoaded = false; + boolean imageFailed = false; + Image image = null; + int width = -1; + int depth = -1; + + /** + *

Constructor for ImageIntrinsics

+ */ + public ImageIntrinsics(String imageFn) { + image = Toolkit.getDefaultToolkit().getImage (imageFn); + width = image.getWidth(this); + + while (!imageFailed && (width == -1 || depth == -1)) { + try { + java.lang.Thread.currentThread().sleep(50); + } catch (Exception e) { + // nop; + } + width = image.getWidth(this); + depth = image.getHeight(this); + } + + if (imageFailed) { + // Maybe it's an EPS or PDF? + // FIXME: this code is crude + BufferedReader ir = null; + String line = null; + int lineLimit = 100; + + try { + ir = new BufferedReader(new FileReader(new File(imageFn))); + line = ir.readLine(); + + if (line != null && line.startsWith("%PDF-")) { + // We've got a PDF! + while (lineLimit > 0 && line != null) { + lineLimit--; + if (line.startsWith("/CropBox [")) { + line = line.substring(10); + if (line.indexOf("]") >= 0) { + line = line.substring(0, line.indexOf("]")); + } + parseBox(line); + lineLimit = 0; + } + line = ir.readLine(); + } + } else if (line != null && line.startsWith("%!") && line.indexOf(" EPSF-") > 0) { + // We've got an EPS! + while (lineLimit > 0 && line != null) { + lineLimit--; + if (line.startsWith("%%BoundingBox: ")) { + line = line.substring(15); + parseBox(line); + lineLimit = 0; + } + line = ir.readLine(); + } + } + } catch (Exception e) { + // nop; + } + + if (ir != null) { + try { + ir.close(); + } catch (Exception e) { + // nop; + } + } + } + } + + public int getWidth(int defaultWidth) { + if (width >= 0) { + return width; + } else { + return defaultWidth; + } + } + + public int getDepth(int defaultDepth) { + if (depth >= 0) { + return depth; + } else { + return defaultDepth; + } + } + + private void parseBox(String line) { + int [] corners = new int [4]; + int count = 0; + + StringTokenizer st = new StringTokenizer(line); + while (count < 4 && st.hasMoreTokens()) { + try { + corners[count++] = Integer.parseInt(st.nextToken()); + } catch (Exception e) { + // nop; + } + } + + width = corners[2] - corners[0]; + depth = corners[3] - corners[1]; + } + + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + if ((infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) { + imageFailed = true; + return false; + } + + // I really only care about the width and height, but if I return false as + // soon as those are available, the BufferedInputStream behind the loader + // gets closed too early. + int flags = ImageObserver.ALLBITS; + if ((infoflags & flags) == flags) { + return false; + } else { + return true; + } + } +} diff --git a/xsl/extensions/xalan2/com/nwalsh/xalan/ImageIntrinsics.java b/xsl/extensions/xalan2/com/nwalsh/xalan/ImageIntrinsics.java new file mode 100644 index 000000000..e4dca7145 --- /dev/null +++ b/xsl/extensions/xalan2/com/nwalsh/xalan/ImageIntrinsics.java @@ -0,0 +1,159 @@ +package com.nwalsh.xalan; + +import java.io.*; +import java.awt.*; +import java.awt.image.*; +import java.lang.Thread; +import java.util.StringTokenizer; + +import org.apache.xalan.extensions.ExpressionContext; + +/** + *

Saxon extension to examine intrinsic size of images

+ * + *

$Id$

+ * + *

Copyright (C) 2002 Norman Walsh.

+ * + *

This class provides a + * Xalan + * extension to find the intrinsic size of images.

+ * + *

Change Log:

+ *
+ *
1.0
+ *

Initial release.

+ *
+ * + * @author Norman Walsh + * ndw@nwalsh.com + * + * @version $Id$ + * + */ +public class ImageIntrinsics implements ImageObserver { + boolean imageLoaded = false; + boolean imageFailed = false; + Image image = null; + int width = -1; + int depth = -1; + + /** + *

Constructor for ImageIntrinsics

+ */ + public ImageIntrinsics(ExpressionContext context, String imageFn) { + image = Toolkit.getDefaultToolkit().getImage (imageFn); + width = image.getWidth(this); + + while (!imageFailed && (width == -1 || depth == -1)) { + try { + java.lang.Thread.currentThread().sleep(50); + } catch (Exception e) { + // nop; + } + width = image.getWidth(this); + depth = image.getHeight(this); + } + + if (imageFailed) { + // Maybe it's an EPS or PDF? + // FIXME: this code is crude + BufferedReader ir = null; + String line = null; + int lineLimit = 100; + + try { + ir = new BufferedReader(new FileReader(new File(imageFn))); + line = ir.readLine(); + + if (line != null && line.startsWith("%PDF-")) { + // We've got a PDF! + while (lineLimit > 0 && line != null) { + lineLimit--; + if (line.startsWith("/CropBox [")) { + line = line.substring(10); + if (line.indexOf("]") >= 0) { + line = line.substring(0, line.indexOf("]")); + } + parseBox(line); + lineLimit = 0; + } + line = ir.readLine(); + } + } else if (line != null && line.startsWith("%!") && line.indexOf(" EPSF-") > 0) { + // We've got an EPS! + while (lineLimit > 0 && line != null) { + lineLimit--; + if (line.startsWith("%%BoundingBox: ")) { + line = line.substring(15); + parseBox(line); + lineLimit = 0; + } + line = ir.readLine(); + } + } + } catch (Exception e) { + // nop; + } + + if (ir != null) { + try { + ir.close(); + } catch (Exception e) { + // nop; + } + } + } + } + + public int getWidth(ExpressionContext context, int defaultWidth) { + if (width >= 0) { + return width; + } else { + return defaultWidth; + } + } + + public int getDepth(ExpressionContext context, int defaultDepth) { + if (depth >= 0) { + return depth; + } else { + return defaultDepth; + } + } + + private void parseBox(String line) { + int [] corners = new int [4]; + int count = 0; + + StringTokenizer st = new StringTokenizer(line); + while (count < 4 && st.hasMoreTokens()) { + try { + corners[count++] = Integer.parseInt(st.nextToken()); + } catch (Exception e) { + // nop; + } + } + + width = corners[2] - corners[0]; + depth = corners[3] - corners[1]; + } + + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + if ((infoflags & ImageObserver.ERROR) == ImageObserver.ERROR) { + imageFailed = true; + return false; + } + + // I really only care about the width and height, but if I return false as + // soon as those are available, the BufferedInputStream behind the loader + // gets closed too early. + int flags = ImageObserver.ALLBITS; + if ((infoflags & flags) == flags) { + return false; + } else { + return true; + } + } +}