Login  Register

Re: Patch for ImageJ that fixes a HeadlessException error

Posted by dscho on Jul 19, 2007; 2:11am
URL: http://imagej.273.s1.nabble.com/Patch-for-ImageJ-that-fixes-a-HeadlessException-error-tp3698809p3698811.html

Hi,

On Wed, 18 Jul 2007, Victor Petrov wrote:

> I would like to submit a patch that corrects ImageJ's behavior when
> running in headless mode (i.e. with java -Djava.awt.headless=true).

Great!

> I have attached the modified source code of ImageProcessor.java.

Here is a patch, made from your file diff'ed vs 1.37u, and my comments
interspersed (a patch shows so called "hunks", where a space at the
beginning of the line means that both old and new versions have this
line, a "+" means that only the new version has it, and "-" means that
only the old version has it):

diff --git a/ij/process/ImageProcessor.java b/ij/process/ImageProcessor.java
index ebbc201..911853e 100644
--- a/ij/process/ImageProcessor.java
+++ b/ij/process/ImageProcessor.java
@@ -3,6 +3,7 @@ package ij.process;
 import java.util.*;
 import java.awt.*;
 import java.awt.image.*;
+import java.awt.FontMetrics;
 import java.lang.reflect.*;
 import ij.gui.*;
 import ij.util.Java2;
@@ -44,6 +45,7 @@ public abstract class ImageProcessor extends Object {
  protected boolean antialiasedText;
  protected boolean boldFont;
  static Frame frame;
+ static Graphics graphics;
 
     ProgressBar progressBar;
     boolean pixelsModified;
@@ -856,6 +858,21 @@ public abstract class ImageProcessor extends Object {
     private ImageProcessor dotMask;
 
  private void setupFrame() {
+
+ if (System.getProperty("java.awt.headless").equalsIgnoreCase("true"))
+ {
+ if (image==null)
+ image=new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
+
+ if (font==null)
+ font = new Font("SansSerif", Font.PLAIN, 12);
+ if (fontMetrics==null)
+ {
+ Graphics g=image.getGraphics();
+ fontMetrics=g.getFontMetrics(font);
+ }
+ return;
+ }

What about the variable "graphics"?

  if (frame==null) {
  frame = new Frame();
  frame.pack();
@@ -873,9 +890,12 @@ public abstract class ImageProcessor extends Object {
  public void drawString(String s) {
  if (s.equals(""))
  return;
+ //If running in headless mode, setupFrame generates HeadlessException,
+ //therefore setupFrame should only run when java.awt.headless is not defined
  setupFrame();
  if (ij.IJ.isMacOSX())
  s += " ";
+
  int w =  getStringWidth(s);
  int cxx = cx;
  if (justification==CENTER_JUSTIFY)
@@ -885,10 +905,11 @@ public abstract class ImageProcessor extends Object {
  int h =  fontMetrics.getHeight();
  if (w<=0 || h<=0) return;
  Image img;
- if (ij.IJ.isLinux() && ij.IJ.isJava2())
+ if ((ij.IJ.isLinux() && ij.IJ.isJava2()) || (System.getProperty("java.awt.headless").equalsIgnoreCase("true")))
  img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  else
  img = frame.createImage(w, h);
+
  Graphics g = img.getGraphics();
  FontMetrics metrics = g.getFontMetrics(font);
  int fontHeight = metrics.getHeight();
@@ -972,7 +993,10 @@ public abstract class ImageProcessor extends Object {
  setupFrame();
  int w;
  if (antialiasedText) {
- Graphics g = frame.getGraphics();
+ //Graphics g = frame.getGraphics();
+ //-BEGIN-CHANGE
+ Graphics g=graphics;
+ //-END-CHANGE

I do not see any code that could possibly set the variable "graphics".  
Have you tested this with antialiasedText==true?

But it is good that someone is working on that.  Thank you very much!

Ciao,
Dscho