From 16a49c1c9d30507b1c135ab528fc10e05eeda5bd Mon Sep 17 00:00:00 2001 From: orbiter Date: Sun, 9 Oct 2005 14:46:33 +0000 Subject: [PATCH] fix for graphics generation bug, see http://www.yacy-forum.de/viewtopic.php?p=10987#10987 git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@886 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/http/httpdFileHandler.java | 31 +++++---- source/de/anomic/tools/ImagePainter.java | 74 +++++++++++++-------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/source/de/anomic/http/httpdFileHandler.java b/source/de/anomic/http/httpdFileHandler.java index 22faa3582..744c1634f 100644 --- a/source/de/anomic/http/httpdFileHandler.java +++ b/source/de/anomic/http/httpdFileHandler.java @@ -417,19 +417,24 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http e.getTargetException().getMessage(),e); targetClass = null; } - targetDate = new Date(System.currentTimeMillis()); - String mimeType = mimeTable.getProperty(targetExt,"text/html"); - - // generate an byte array from the generated image - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ImageIO.write(bi, targetExt, baos); - byte[] result = baos.toByteArray(); - - // write the array to the client - httpd.sendRespondHeader(this.connectionProperties, out, "HTTP/1.1", 200, null, mimeType, result.length, targetDate, null, null, null, null); - Thread.currentThread().sleep(200); // see below - serverFileUtils.write(result, out); - + if (bi == null) { + // error with image generation; send file-not-found + httpd.sendRespondError(this.connectionProperties,out,3,404,"File not Found",null,null); + } else { + // send an image to client + targetDate = new Date(System.currentTimeMillis()); + String mimeType = mimeTable.getProperty(targetExt,"text/html"); + + // generate an byte array from the generated image + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(bi, targetExt, baos); + byte[] result = baos.toByteArray(); + + // write the array to the client + httpd.sendRespondHeader(this.connectionProperties, out, "HTTP/1.1", 200, null, mimeType, result.length, targetDate, null, null, null, null); + Thread.currentThread().sleep(200); // see below + serverFileUtils.write(result, out); + } } else if ((targetFile.exists()) && (targetFile.canRead())) { // we have found a file that can be written to the client // if this file uses templates, then we use the template diff --git a/source/de/anomic/tools/ImagePainter.java b/source/de/anomic/tools/ImagePainter.java index eb469ca8c..b6e1d68bc 100644 --- a/source/de/anomic/tools/ImagePainter.java +++ b/source/de/anomic/tools/ImagePainter.java @@ -349,39 +349,59 @@ public class ImagePainter { GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); BufferedImage bi = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT); - */ - BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics2D gr = bi.createGraphics(); - gr.setBackground(Color.white); - gr.clearRect(0, 0, width, height); - - WritableRaster wr = bi.getRaster(); - long c; - if (complementary) { - int r, g, b; - // then set pixels - for (int i = width - 1; i >= 0; i--) { - for (int j = height - 1; j >= 0; j--) { - c = grid[i + j * width]; - if (c >= 0) { - r = (int) (c >> 16); - g = (int) ((c >> 8) & 0xff); - b = (int) (c & 0xff); - wr.setPixel(i, j, new int[]{(0x1fe - g - b) / 2, (0x1fe - r - b) / 2, (0x1fe - r - g) / 2}); + */ + try { + BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D gr = bi.createGraphics(); + gr.setBackground(Color.white); + gr.clearRect(0, 0, width, height); + + WritableRaster wr = bi.getRaster(); + long c; + if (complementary) { + int r, g, b; + // then set pixels + for (int i = width - 1; i >= 0; i--) { + for (int j = height - 1; j >= 0; j--) { + c = grid[i + j * width]; + if (c >= 0) { + r = (int) (c >> 16); + g = (int) ((c >> 8) & 0xff); + b = (int) (c & 0xff); + wr.setPixel(i, j, new int[]{(0x1fe - g - b) / 2, (0x1fe - r - b) / 2, (0x1fe - r - g) / 2}); + } } } - } - } else { - for (int i = width - 1; i >= 0; i--) { - for (int j = height - 1; j >= 0; j--) { - c = grid[i + j * width]; - if (c >= 0) { - wr.setPixel(i, j, new int[]{(int) (c >> 16), (int) ((c >> 8) & 0xff), (int) (c & 0xff)}); + } else { + for (int i = width - 1; i >= 0; i--) { + for (int j = height - 1; j >= 0; j--) { + c = grid[i + j * width]; + if (c >= 0) { + wr.setPixel(i, j, new int[]{(int) (c >> 16), (int) ((c >> 8) & 0xff), (int) (c & 0xff)}); + } } } } + return bi; + } catch (Exception e) { + // strange case where environment disallowes generation of graphics + /* + java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable. + at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method) + at sun.awt.X11GraphicsEnvironment.access$000(X11GraphicsEnvironment.java:53) + at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:142) + at java.security.AccessController.doPrivileged(Native Method) + at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:131) + at java.lang.Class.forName0(Native Method) + at java.lang.Class.forName(Class.java:164) + at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:68) + at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1141) + at de.anomic.tools.ImagePainter.toImage(ImagePainter.java:354) + */ + System.out.println("Error with Graphics environment:"); + e.printStackTrace(); + return new BufferedImage(0, 0, BufferedImage.TYPE_INT_RGB); } - return bi; } }